Solve the Rotten Oranges problem

Problem

Rotten Oranges: in a grid of fresh (1), rotten (2), and empty (0) cells, each minute every rotten orange rots its 4-directional fresh neighbours. Return the minutes until no fresh orange remains, or -1 if some fresh orange can never rot.

Input / Output

  • Input: an m x n grid with values 0 (empty), 1 (fresh), 2 (rotten).
  • Output: the number of minutes until no fresh orange remains, or -1 if impossible.

Constraints

  • 1 ≤ m, n ≤ 10^3.
  • Rot spreads only to 4-directional neighbours, one layer per minute.

Example

[[2,1,1],
 [1,1,0],
 [0,1,1]] -> 4
asked …
LeaderboardSalaryAccount