994. Rotting Oranges
Asked at 47Amazon73Microsoft58Bloomberg56Google51Meta41Apple40OpenAI81Flipkart80Anduril63Infosys61Salesforce41Goldman Sachs40Roblox37Lyft100Rakuten88Wix87Sigmoid87Moloco81Tekion74Commvault71Zoho70Dream1168Zoox66Myntra64eBay64Samsung63Walmart Labs60ByteDance59Adobe55Oracle55ServiceNow55Expedia54Uber52IBM50TikTok50Cognizant48Snowflake48DE Shaw47Databricks44Intuit43PayPal42Nvidia39Pinterest36DoorDash29Yandex28LinkedIn27tcs27
Problem
In the grid, 0 is empty, 1 is a fresh orange and 2 is a rotten one. Every minute, each fresh orange next to a rotten one (up, down, left or right) rots. Return the minutes until no fresh orange remains, or −1 if some never rot.
Example: [[2,1,1],[1,1,0],[0,1,1]] → 4; [[2,1,1],[0,1,1],[1,0,1]] → -1
Multi-source BFS from all rotten oranges at once; each BFS layer = 1 minute. At the end, any fresh orange left → −1.
minute 0 1 2 3 4
2 1 1 2 2 1 2 2 2 2 2 2 2 2 2
1 1 0 -> 2 1 0 2 2 0 2 2 0 2 2 0
0 1 1 0 1 1 0 1 1 0 2 1 0 2 2
Complexity: O(R·C).
class Solution:
def orangesRotting(self, grid: List[List[int]]) -> int:
R, C = len(grid), len(grid[0])
q = deque((r, c) for r in range(R) for c in range(C) if grid[r][c] == 2)
fresh = sum(row.count(1) for row in grid)
minutes = 0
while q and fresh:
minutes += 1
for _ in range(len(q)):
r, c = q.popleft()
for nr, nc in ((r + 1, c), (r - 1, c), (r, c + 1), (r, c - 1)):
if 0 <= nr < R and 0 <= nc < C and grid[nr][nc] == 1:
grid[nr][nc] = 2
fresh -= 1
q.append((nr, nc))
return -1 if fresh else minutes
assert Solution().orangesRotting([[2, 1, 1], [1, 1, 0], [0, 1, 1]]) == 4
assert Solution().orangesRotting([[2, 1, 1], [0, 1, 1], [1, 0, 1]]) == -1
assert Solution().orangesRotting([[0, 2]]) == 0
class Solution {
public int orangesRotting(int[][] grid) {
int R = grid.length, C = grid[0].length, fresh = 0, minutes = 0;
Deque<int[]> q = new ArrayDeque<>();
for (int r = 0; r < R; r++)
for (int c = 0; c < C; c++) {
if (grid[r][c] == 2) q.add(new int[]{r, c});
else if (grid[r][c] == 1) fresh++;
}
int[][] dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
while (!q.isEmpty() && fresh > 0) {
minutes++;
for (int i = q.size(); i > 0; i--) {
int[] cur = q.poll();
for (int[] d : dirs) {
int nr = cur[0] + d[0], nc = cur[1] + d[1];
if (nr >= 0 && nc >= 0 && nr < R && nc < C && grid[nr][nc] == 1) {
grid[nr][nc] = 2; fresh--; q.add(new int[]{nr, nc});
}
}
}
}
return fresh > 0 ? -1 : minutes;
}
}
func orangesRotting(grid [][]int) int {
R, C := len(grid), len(grid[0])
q, fresh := [][2]int{}, 0
for r := 0; r < R; r++ {
for c := 0; c < C; c++ {
if grid[r][c] == 2 {
q = append(q, [2]int{r, c})
} else if grid[r][c] == 1 {
fresh++
}
}
}
minutes := 0
for len(q) > 0 && fresh > 0 { // multi-source BFS, one level per minute
minutes++
next := [][2]int{}
for _, p := range q {
for _, d := range [][2]int{{1, 0}, {-1, 0}, {0, 1}, {0, -1}} {
nr, nc := p[0]+d[0], p[1]+d[1]
if nr >= 0 && nr < R && nc >= 0 && nc < C && grid[nr][nc] == 1 {
grid[nr][nc] = 2
fresh--
next = append(next, [2]int{nr, nc})
}
}
}
q = next
}
if fresh > 0 {
return -1
}
return minutes
}