Problem
You are given an m x n
grid
where each cell can have one of three values:
0
representing an empty cell,1
representing a fresh orange, or2
representing a rotten orange.
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
.
Examples
Example 1:
$$ \begin{array}{c|c|c} \textbf{Minute 0} & \textbf{Minute 1} & \textbf{Minute 2} \\ \left[ \begin{array}{ccc} \colorbox{red} 2 & \colorbox{orange} 1 & \colorbox{orange} 1 \\ \colorbox{orange} 1 & \colorbox{orange} 1 & \colorbox{gray} 0 \\ \colorbox{gray} 0 & \colorbox{orange} 1 & \colorbox{orange} 1 \end{array} \right] & \left[ \begin{array}{ccc} \colorbox{red} 2 & \colorbox{red} 2 & \colorbox{orange} 1 \\ \colorbox{red} 2 & \colorbox{orange} 1 & \colorbox{gray} 0 \\ \colorbox{gray} 0 & \colorbox{orange} 1 & \colorbox{orange} 1 \end{array} \right] & \left[ \begin{array}{ccc} \colorbox{red} 2 & \colorbox{red} 2 & \colorbox{red} 2 \\ \colorbox{red} 2 & \colorbox{red} 2 & \colorbox{gray} 0 \\ \colorbox{gray} 0 & \colorbox{orange} 1 & \colorbox{orange} 1 \end{array} \right] \\ \hline \textbf{Minute 3} & \textbf{Minute 4} & \\ \left[ \begin{array}{ccc} \colorbox{red} 2 & \colorbox{red} 2 & \colorbox{red} 2 \\ \colorbox{red} 2 & \colorbox{red} 2 & \colorbox{gray} 0 \\ \colorbox{gray} 0 & \colorbox{red} 2 & \colorbox{orange} 1 \end{array} \right] & \left[ \begin{array}{ccc} \colorbox{red} 2 & \colorbox{red} 2 & \colorbox{red} 2 \\ \colorbox{red} 2 & \colorbox{red} 2 & \colorbox{gray} 0 \\ \colorbox{gray} 0 & \colorbox{red} 2 & \colorbox{red} 2 \end{array} \right] & \end{array} $$
|
|
Example 2:
|
|
Example 3:
|
|
Solution
Method 1 - BFS with Level by Level Approach
The problem can be visualised as a graph traversal problem where:
- Each orange in the grid serves as a node.
- The adjacency of nodes corresponds to the 4-directional neighbours in the grid.
We can use BFS around rotten oranges. Basically we have to count BFS waves, which is same as number of levels when we iterate on Tree level by level. Also, we should keep track of fresh oranges, as if there are fresh oranges remaining at the end of BFS, we should just return.
By using a breadth-first search (BFS) approach starting from all initially rotten oranges, we track:
- The spread of rot minute by minute.
- If any fresh orange remains unreachable, we return
-1
.
Key points:
- Use a queue to process rotten oranges in layers, as BFS computes the shortest path for graph traversal.
- Count the total fresh oranges initially.
- If after BFS traversal some fresh oranges are still left, the answer is
-1
.
Approach
- Initial Setup:
- Count the number of fresh oranges.
- Add all rotten oranges to a queue.
- Track time elapsed during the propagation.
- Breadth-First Search (BFS):
- For each rotten orange, rot its adjacent fresh oranges.
- Track which oranges become rotten during each minute.
- Exit Conditions:
- If the queue is empty but there are fresh oranges remaining, return
-1
. - Otherwise, return the total time elapsed.
- If the queue is empty but there are fresh oranges remaining, return
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(m * n)
. Every orange is visited once during BFS. - 🧺 Space complexity:
O(m * n)
. The queue may contain all rotten oranges.