Rotting Oranges

Medium

Topics
GraphBFSMatrix

In an m x n grid each cell is 0 (empty), 1 (fresh orange), or 2 (rotten). Every minute, a fresh orange adjacent (4-directionally) to a rotten one becomes rotten. Return the minutes until no fresh orange remains, or -1 if impossible.

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

Constraints

  • 1 <= m, n <= 10
  • grid[i][j] is 0, 1, or 2.
Run ⌘' · Submit ⌘⏎