Rotting Oranges
MediumGraphs
Decode it first. Clues, constraints, pattern and plan before any code.A crate is a grid where 0 is an empty slot, 1 is a fresh orange and 2 is a rotten one. Every minute, each rotten orange rots the fresh oranges directly above, below, left and right of it. Return the number of minutes until no fresh orange is left, or -1 if some fresh orange can never be reached.
[[2, 1, 1],
[1, 1, 0],
[0, 1, 1]] → 4
[[2, 1, 1],
[0, 1, 1],
[1, 0, 1]] → -1 (bottom-left orange is cut off)
[[0, 2]] → 0 (nothing fresh to begin with)Constraints
1 <= grid.length, grid[i].length <= 10grid[i][j]is0,1or2