Skip to problem
←

Rotting Oranges

Medium 0 attempts

Given a rectangular grid, each cell is 0 (empty), 1 (fresh orange), or 2 (rotten orange). Every minute, all currently rotten oranges simultaneously infect fresh oranges directly above, below, left, or right of them.

Newly rotten oranges spread rot starting in the next minute. Rot cannot move diagonally or pass through empty cells.

Return the minimum minutes until no fresh oranges remain. Return 0 if there are none initially, even if the grid has no oranges, and -1 if any fresh orange can never be reached. You may modify the grid.

For [[2, 1, 1, 1, 2]], rot spreads from both ends at once, so the answer is 2, not 3. For [[2, 0, 1]], the empty cell blocks the only route and the answer is -1.

Function contract

Implement orangesRotting(grid).

Output: Return the minutes until all fresh oranges rot, 0 if none are initially fresh, or -1 if any remain unreachable.

Example 1

Input
grid = [[2,1,1],[1,1,0],[0,1,1]]
Output
4

Example 2

Input
grid = [[1]]
Output
-1
Constraints
  • 1 <= grid.length, grid[0].length <= 500; the grid is rectangular.
  • 0 <= grid[i][j] <= 2

Breadth-First Search from Every Rotten Orange

Collect all initially rotten cells into one queue and count fresh oranges. Starting all sources together models simultaneous spreading and gives the earliest infection time for every reachable cell.

At each minute, process only the queue entries present at the start of that layer. Mark a fresh neighbor rotten as soon as it is enqueued, preventing multiple neighboring sources from counting it twice. Newly enqueued cells wait for the next layer before spreading further.

Stop when no fresh oranges remain or the queue is exhausted. Remaining fresh oranges are unreachable and require -1. If none were fresh initially, return zero without advancing time.

A head index advances through the queue without repeatedly shifting array contents. Each cell is enqueued at most once.

javascript
function orangesRotting(grid) {
  const m = grid.length,
    n = grid[0].length,
    q = [];
  let fresh = 0;
  for (let i = 0; i < m; i++)
    for (let j = 0; j < n; j++) {
      if (grid[i][j] === 2) q.push([i, j]);
      if (grid[i][j] === 1) fresh++;
    }
  if (!fresh) return 0;
  const dirs = [
    [0, 1],
    [0, -1],
    [1, 0],
    [-1, 0],
  ];
  let time = 0;
  let head = 0;
  while (head < q.length && fresh) {
    time++;
    const size = q.length - head;
    for (let k = 0; k < size; k++) {
      const [r, c] = q[head++];
      for (const [dr, dc] of dirs) {
        const nr = r + dr,
          nc = c + dc;
        if (nr >= 0 && nr < m && nc >= 0 && nc < n && grid[nr][nc] === 1) {
          grid[nr][nc] = 2;
          fresh--;
          q.push([nr, nc]);
        }
      }
    }
  }
  return fresh ? -1 : time;
}

Time: O(mn) | Space: O(mn)

Loading discussion…

Saved in this browser only. Private to you.

JavaScript