medium

Rotting Oranges

A grid holds empty cells, fresh oranges and rotten oranges. Every minute each rotten orange rots its fresh 4-directional neighbours. Return the minimum number of minutes until no fresh orange remains, or -1 if some orange can never rot.

Constraints
  • 1 ≤ m, n ≤ 10
  • grid[i][j] ∈ {0, 1, 2}
Examples
in: grid = [[2,1,1],[1,1,0],[0,1,1]]
out: 4
in: grid = [[2,1,1],[0,1,1],[1,0,1]]
out: -1

Code it yourself

Solve in
Test execution is not yet available for this exercise.Practice journal →Draft saved in this browser.
Hints:
Which approach applies?

Choose an approach to check your pattern recognition, or reveal the discussion when you need help.