Try it first — give it 25 minutes
0/4 revealedStuck? Take one hint at a time. The struggle is where the pattern sticks.
The problem in plain English
You get an m × n grid. Each cell is:
0: empty,1: a fresh orange,2: a rotten orange.
Every minute, every fresh orange that is directly up, down, left or right of a rotten orange becomes rotten. Return the minimum number of minutes until no fresh orange remains. If some fresh orange can never rot, return -1. If there are no fresh oranges to begin with, the answer is 0.
Limits: the grid is at most 10 × 10.
Example:
2 1 1
1 1 0
0 1 1The answer is 4. The rot spreads one ring per minute from the top-left corner and reaches the bottom-right orange last.
Start with brute force
Simulate it literally. Each minute, scan the whole grid and find every fresh orange next to a rotten one. Rot them all at once (collect them first, so an orange that rots this minute doesn't spread rot in the same minute). Stop when a minute changes nothing.
Each minute costs O(m·n), and the process can last up to O(m·n) minutes (picture a long snake of oranges). That's O((m·n)²). On a 10 × 10 grid it's fine. But it rescans thousands of cells that can't change. The optimal version only looks at the oranges that just rotted.
Building the intuition
Watch the example. At minute 0 only the corner is rotten. At minute 1 its neighbours rot. At minute 2 their neighbours rot. The rot moves outward one step per minute, like ripples in a pond.
"Spreads outward one step at a time" is exactly how BFS works. BFS visits everything at distance 1 first, then everything at distance 2, and so on. The minute an orange rots is just its distance from the nearest rotten orange.
What if there are several rotten oranges at the start? They all spread at the same time. If you ran one BFS per rotten orange, you'd have to merge the results and take, for each orange, the earliest time. That's messy and slow.
The clean answer is multi-source BFS: put all the starting rotten oranges in the queue at minute 0, then run one ordinary BFS. The queue naturally holds the minute-0 oranges, then all the minute-1 oranges, then minute 2, and so on. It's like dropping several stones into the pond at once.
Two more pieces:
- Counting minutes. Process the queue level by level. Read the queue's size, pop exactly that many oranges (they all rotted in the same minute), push their fresh neighbours, then add 1 to the minute count.
- Detecting -1. Count the fresh oranges before you start. Subtract 1 each time one rots. If the count is still above 0 when the BFS ends, some orange was walled off by empty cells.
The optimal approach
- Scan the grid once. Push every
2onto the queue, and count every1intofresh. minutes = 0. While the queue isn't empty andfresh > 0:- Let
sizebe the queue's current length. Popsizecells. - For each popped cell, for each of the 4 neighbours that's inside the grid and equal to
1: set it to2, dofresh -= 1, and push it. minutes += 1.
- Let
- Return
minutesiffresh == 0, else-1.
The subtle detail is the fresh > 0 condition on the loop. Without it, the last level of oranges is popped, finds nothing new, and still adds 1 to minutes. You'd return 5 instead of 4 on the example. Stopping as soon as nothing fresh is left avoids that off-by-one, and it also makes "no fresh oranges at all" return 0 without a special case.
The code
Every solution below is tested against the examples and edge cases before it ships. Pick your language — we'll remember it.
from collections import deque
class Solution:
def orangesRotting(self, grid: List[List[int]]) -> int:
rows, cols = len(grid), len(grid[0])
directions = ((1, 0), (-1, 0), (0, 1), (0, -1)) # down, up, right, left
# Multi-source BFS: every rotten orange starts in the queue at minute 0
queue = deque()
fresh = 0
for r in range(rows):
for c in range(cols):
if grid[r][c] == 2:
queue.append((r, c))
elif grid[r][c] == 1:
fresh += 1
minutes = 0
while queue and fresh > 0:
# Everything in the queue right now rotted at the same minute: process that whole level
for _ in range(len(queue)):
r, c = queue.popleft()
for dr, dc in directions:
nr, nc = r + dr, c + dc
if 0 <= nr < rows and 0 <= nc < cols and grid[nr][nc] == 1:
grid[nr][nc] = 2 # rot it now, so it is queued only once
fresh -= 1
queue.append((nr, nc))
minutes += 1
return minutes if fresh == 0 else -1class Solution {
public:
int orangesRotting(vector<vector<int>>& grid) {
int rows = grid.size(), cols = grid[0].size();
const int dr[4] = {1, -1, 0, 0}; // down, up, right, left
const int dc[4] = {0, 0, 1, -1};
// Multi-source BFS: every rotten orange starts in the queue at minute 0
queue<pair<int, int>> q;
int fresh = 0;
for (int r = 0; r < rows; r++)
for (int c = 0; c < cols; c++) {
if (grid[r][c] == 2) q.push({r, c});
else if (grid[r][c] == 1) fresh++;
}
int minutes = 0;
while (!q.empty() && fresh > 0) {
// Everything in the queue right now rotted at the same minute: process that whole level
int levelSize = q.size();
for (int i = 0; i < levelSize; i++) {
auto [r, c] = q.front();
q.pop();
for (int d = 0; d < 4; d++) {
int nr = r + dr[d], nc = c + dc[d];
if (nr >= 0 && nr < rows && nc >= 0 && nc < cols && grid[nr][nc] == 1) {
grid[nr][nc] = 2; // rot it now, so it is queued only once
fresh--;
q.push({nr, nc});
}
}
}
minutes++;
}
return fresh == 0 ? minutes : -1;
}
};class Solution {
private static final int[][] DIRECTIONS = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } }; // down, up, right, left
public int orangesRotting(int[][] grid) {
int rows = grid.length, cols = grid[0].length;
// Multi-source BFS: every rotten orange starts in the queue at minute 0
ArrayDeque<int[]> queue = new ArrayDeque<>();
int fresh = 0;
for (int r = 0; r < rows; r++)
for (int c = 0; c < cols; c++) {
if (grid[r][c] == 2) queue.add(new int[] { r, c });
else if (grid[r][c] == 1) fresh++;
}
int minutes = 0;
while (!queue.isEmpty() && fresh > 0) {
// Everything in the queue right now rotted at the same minute: process that whole level
int levelSize = queue.size();
for (int i = 0; i < levelSize; i++) {
int[] cell = queue.poll();
for (int[] d : DIRECTIONS) {
int nr = cell[0] + d[0], nc = cell[1] + d[1];
if (nr >= 0 && nr < rows && nc >= 0 && nc < cols && grid[nr][nc] == 1) {
grid[nr][nc] = 2; // rot it now, so it is queued only once
fresh--;
queue.add(new int[] { nr, nc });
}
}
}
minutes++;
}
return fresh == 0 ? minutes : -1;
}
}/**
* @param {number[][]} grid
* @return {number}
*/
var orangesRotting = function (grid) {
const rows = grid.length, cols = grid[0].length;
const directions = [[1, 0], [-1, 0], [0, 1], [0, -1]]; // down, up, right, left
// Multi-source BFS: every rotten orange starts in the queue at minute 0
let queue = [];
let fresh = 0;
for (let r = 0; r < rows; r++)
for (let c = 0; c < cols; c++) {
if (grid[r][c] === 2) queue.push([r, c]);
else if (grid[r][c] === 1) fresh++;
}
let minutes = 0;
while (queue.length > 0 && fresh > 0) {
// Everything in the queue right now rotted at the same minute: process that whole level
const next = [];
for (const [r, c] of queue) {
for (const [dr, dc] of directions) {
const nr = r + dr, nc = c + dc;
if (nr >= 0 && nr < rows && nc >= 0 && nc < cols && grid[nr][nc] === 1) {
grid[nr][nc] = 2; // rot it now, so it is queued only once
fresh--;
next.push([nr, nc]);
}
}
}
queue = next;
minutes++;
}
return fresh === 0 ? minutes : -1;
};Dry run
The example grid. Fresh oranges at the start: 6. The queue holds (0,0).
| Minute | Popped this level | Newly rotten | fresh after |
|---|---|---|---|
| 1 | (0,0) | (1,0), (0,1) | 4 |
| 2 | (1,0), (0,1) | (1,1), (0,2) | 2 |
| 3 | (1,1), (0,2) | (2,1) | 1 |
| 4 | (2,1) | (2,2) | 0 |
Now fresh == 0, so the loop stops with minutes = 4. The queue still holds (2,2), but it doesn't matter: nothing is left to rot.
For [[2,1,1],[0,1,1],[1,0,1]], the orange at (2,0) has an empty cell above it and an empty cell to its right. The BFS ends with fresh = 1, so the answer is -1.
Complexity
- Time: O(m · n). One scan to fill the queue, then each orange is pushed and popped at most once, with 4 neighbour checks each.
- Space: O(m · n) for the queue in the worst case. The grid itself stores the rotten state, so no visited array is needed.
Edge cases and traps
- No fresh oranges.
[[0, 2]]or[[0]]must return0. The loop never runs, becausefreshis 0. - Fresh oranges but nothing rotten.
[[1]]returns-1. The queue starts empty andfreshstays at 1. - The extra minute at the end. Counting a level that rots nothing gives an answer that's one too big. Guard the loop with
fresh > 0. - Rotting on pop instead of on push. An orange could be pushed twice in the same minute and decrement
freshtwice. Set it to2the moment you push it. - One BFS per rotten orange. It gives the wrong timing when you just add up results, and it's slower. Use one queue with all the sources.
Say it in the interview
"The rot spreads like a BFS, one layer per minute. And since all the rotten oranges spread at the same time, it's a multi-source BFS: I put every rotten orange in the queue at the start. I also count the fresh oranges. Then I process the queue level by level. Each level is one minute, and I rot each fresh neighbour, push it and decrement the fresh count. I stop when the queue is empty or nothing fresh is left. If fresh is zero I return the minutes; otherwise some orange was unreachable and I return -1. Each cell is handled once, so it's O(m·n) time and space."
Follow-ups interviewers ask
- "What if each cell's distance to the nearest rotten orange is needed?" Same multi-source BFS. Store the level at which each cell was reached. That's LeetCode's 01 Matrix and Walls and Gates.
- "Why does BFS give the minimum time?" BFS reaches cells in order of distance, so the first time it reaches a fresh orange is along a shortest path from the nearest rotten one. That's exactly when the rot would arrive.
- "Can you avoid modifying the grid?" Keep a separate
visitedgrid, or atimegrid initialised to -1, and use it instead of writing2into the input.
Practise the same idea
This is Day 78 of a free 90-day plan 🐸
90 problems in the order that builds on itself, one a day, with a walkthrough like this for each. Tick them off and keep your streak.
Get the full roadmap — free