← DiffPush

Rotting Oranges

Standard Bar Graphs · Traversal Problems O(m * n) · O(m * n)

The Cold-Room Contamination Sweep

A cold room stores produce in a grid of crates: spoiled units, fresh units, and empty slots. Every minute the spoilage jumps to any fresh crate sharing an edge with a spoiled one. The floor manager must know the exact minute the last fresh unit goes down — or hear that some shelf corner is sealed off from every spoiled crate and will never turn.

Input: An m x n grid where 0 is an empty slot, 1 a fresh crate, and 2 a spoiled crate.

Output: Return the minimum number of minutes until no fresh crate remains, or -1 if some fresh crate can never be reached.

Constraints

Examples

Example 1

Input: {"grid":[[2,1,1],[1,1,0],[0,1,1]]}
Output: 4
The spoilage wave needs four minutes to crawl to the far corner.

Example 2

Input: {"grid":[[0,2]]}
Output: 0
Nothing fresh is present, so the clock never needs to start.

Solve this in your browser →

Also on LeetCode ↗