Rotting Oranges
MediumP1Coding
You are given an m x n grid where each cell is 0 (empty), 1 (fresh orange) or 2 (rotten orange). Every minute, each fresh orange that is directly adjacent (up, down, left or right) to a rotten orange becomes rotten.
Return the minimum number of minutes until no fresh orange remains, or -1 if that never happens. If there are no fresh oranges at the start, the answer is 0.
Example 1
- Input
- grid = [[2,1,1],[1,1,0],[0,1,1]]
- Output
- 4
Example 2
- Input
- grid = [[2,1,1],[0,1,1],[1,0,1]]
- Output
- -1
- Why
- The orange in the bottom-left corner is never adjacent to a rotten one.
Example 3
- Input
- grid = [[0,2]]
- Output
- 0
Constraints
- 1 <= m, n <= 10
- grid[i][j] is 0, 1 or 2
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 12. ⌘Enter runs, ⌘⇧Enter submits.