Rotting Oranges
Difficulty: Medium
You're given a grid where each cell is one of:
0- an empty cell1- a fresh orange2- a rotten orange
Every minute, each fresh orange next to a rotten one (horizontally or vertically) becomes rotten. This happens across the whole grid at the same time.
Return the minimum minutes until no fresh orange is left. If some fresh orange can never be reached, return -1.
Examples
Input: grid = [[2,1,1],[1,1,0],[0,1,1]]
Output: 4
Rot spreads outward from the single rotten orange in the top-left, reaching the farthest fresh orange after 4 minutes.
Input: grid = [[2,1,1],[0,1,1],[1,0,1]]
Output: -1
The fresh orange in the bottom-left corner is boxed in by 0s on every side it could rot from, so it can never turn rotten.
Input: grid = [[0,2]]
Output: 0
There are no fresh oranges to begin with, so 0 minutes are needed.
Constraints
1 <= rows, cols <= 10
grid[i][j] is 0, 1, or 2
Approach
"Every rotten orange spreads at the same time" is the giveaway: use a multi-source breadth-first search, a BFS that starts from every rotten orange at once.
Put every rotten orange in the queue (minute 0). Then process the queue in layers: pop each cell, rot its fresh neighbors, and push them for the next layer. BFS works one ring of distance at a time, so each layer is one minute. The number of layers needed is the answer.
If any fresh orange is left after the BFS, it was unreachable, so return -1.
Solutions
Brute Force - Simulate Minute by Minute
Scan the grid for rotten oranges, collect every fresh orange next to one, and rot them all together (one minute). Repeat until no fresh oranges remain, or a scan finds nothing new to rot (the rest are unreachable).
function orangesRotting(grid) {
const rows = grid.length;
const cols = grid[0].length;
let minutes = 0;
function hasFreshOrange() {
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
if (grid[r][c] === 1) return true;
}
}
return false;
}
while (hasFreshOrange()) {
const toRot = [];
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
if (grid[r][c] !== 2) continue;
const neighbors = [[r + 1, c], [r - 1, c], [r, c + 1], [r, c - 1]];
for (const [nr, nc] of neighbors) {
if (nr >= 0 && nr < rows && nc >= 0 && nc < cols && grid[nr][nc] === 1) {
toRot.push([nr, nc]);
}
}
}
}
if (toRot.length === 0) {
return -1; // fresh oranges remain, but nothing new rotted this minute
}
for (const [r, c] of toRot) {
grid[r][c] = 2;
}
minutes++;
}
return minutes;
}Time: O((rows × cols)²) in the worst case · Space: O(rows × cols)
Optimal - Multi-Source BFS
Start the BFS queue with every rotten orange and count the fresh ones. For each popped orange, rot its fresh neighbors, lower the fresh count, and queue each neighbor with the minute it rotted.
The last minute assigned is the answer. If fresh oranges remain when the queue is empty, return -1.
function orangesRotting(grid) {
const rows = grid.length;
const cols = grid[0].length;
const queue = [];
let freshCount = 0;
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
if (grid[r][c] === 2) {
queue.push([r, c, 0]);
} else if (grid[r][c] === 1) {
freshCount++;
}
}
}
if (freshCount === 0) return 0;
let minutes = 0;
while (queue.length > 0) {
const [r, c, time] = queue.shift();
const neighbors = [[r + 1, c], [r - 1, c], [r, c + 1], [r, c - 1]];
for (const [nr, nc] of neighbors) {
if (nr >= 0 && nr < rows && nc >= 0 && nc < cols && grid[nr][nc] === 1) {
grid[nr][nc] = 2;
freshCount--;
minutes = time + 1;
queue.push([nr, nc, time + 1]);
}
}
}
return freshCount === 0 ? minutes : -1;
}Time: O(rows × cols) · Space: O(rows × cols)