dsa · medium
Rotting Oranges
grid cells are 0 (empty), 1 (fresh orange), or 2 (rotten orange). Every minute, every rotten orange turns its 4-directionally adjacent fresh oranges rotten.
Arguments
grid— m by n grid; 0 empty, 1 fresh, 2 rotten
Return the minutes until no fresh orange remains, or -1 if that is impossible.
Example
[[2,1,1],[1,1,0],[0,1,1]] → 4.
[[2,1,1],[0,1,1],[1,0,1]] → -1 (bottom-left fresh orange is trapped).
[[0,2]] → 0 (nothing fresh).
Constraints
1 <= m, n <= 10 grid[i][j] is 0, 1, or 2 Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.
Examples
Example 1
Input: [[2,1,1],[1,1,0],[0,1,1]] Expected: 4
Example 2
Input: [[2,1,1],[0,1,1],[1,0,1]] Expected: -1
Example 3
Input: [[0,2]] Expected: 0