Explanation
Rotting Oranges is a Medium interview problem tagged [Array, BFS, Matrix]. The winning approach is typically **Multi-source BFS**.
#994
Every minute, any fresh orange that is 4-directionally adjacent to a rotten orange becomes rotten.
Return the minimum number of minutes that must elapse until no cell has a fresh orange. If this is impossible, return -1.
Example 1
Input: grid = [[2,1,1],[1,1,0],[0,1,1]]
Output: 4
It takes 4 minutes for all fresh oranges to rot.
Example 2
Input: grid = [[2,1,1],[0,1,1],[1,0,1]]
Output: -1
The bottom-left orange never rots.
Example 3
Input: grid = [[0,2]]
Output: 0
No fresh oranges.
Rotting Oranges is a Medium interview problem tagged [Array, BFS, Matrix]. The winning approach is typically **Multi-source BFS**.
Simultaneous infection/flow frontiers stay O(mn).
Enqueue all sources at once so distance expands in parallel waves.
Pattern-level skeleton only — not this problem’s exact answer. Adapt it to the statement.
Time: O(n log n) or O(n)
Space: O(n)
Before coding, restate the problem, name the pattern (Multi-source BFS), and write the invariant or DP state in one sentence.
Got it?
Mark #994 Rotting Oranges as understood when the pattern clicks.