Rotting Oranges
Rotting Oranges
Problem Statement
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
.
Example
Output: 2
Solution
This problem is a perfect use case for a Breadth-First Search (BFS). Since the rotting process happens in layers (or minutes), BFS is ideal because it explores the graph level by level. Each level of the BFS will correspond to one minute passing.
We can start by finding all initially rotten oranges and adding them to a queue. We also count the number of fresh oranges. Then, we perform a multi-source BFS, where each "level" of the search represents one minute.
Algorithm Steps (BFS)
- Scan the grid to find all initial rotten oranges and add their coordinates to a queue. Also, count the number of fresh oranges.
- Initialize `minutes` to 0.
- While the queue is not empty and there are still fresh oranges:
- Process all oranges currently in the queue (one full level/minute).
- For each rotten orange dequeued, check its 4-directional neighbors.
- If a neighbor is a fresh orange, make it rotten, decrement the `freshOranges` count, and add it to the queue.
- After processing a full level, if any oranges were rotted, increment `minutes`.
- After the loop, if `freshOranges` is 0, return `minutes`. Otherwise, return -1.
Rotting Oranges (BFS)
Minutes Elapsed: 0