Solution: Rotting Oranges
Let’s solve the Rotting Oranges problem using the Breadth-first Search pattern.
We'll cover the following
Statement#
Consider an
, which indicates an unoccupied cell. , representing a freshly picked orange. , indicating a rotten orange.
Any fresh orange that is 4–directionally adjacent to a rotten orange will also turn rotten with each passing minute.
Your task is to determine the minimum time required for all cells to have rotten oranges. In case, this objective cannot be achieved, return
Constraints:
mgrid.lengthngrid[i].lengthm,ngrid[i][j]is0,1, or2.
Solution#
Let’s solve this problem by understanding the nature of the problem first. We are asked to find the minimum time required for all the cells to have rotten oranges. As proposed in the problem statement, the 4–directionally adjacent cells in the grid will rot first. We can think of the rotten oranges as the root nodes and the fresh oranges in other cells as their neighbors. Thus, we can limit our choice for solving this problem using the breadth-first search approach because the rotten oranges will contaminate the neighbor oranges first and then move to the next neighbors in the graph.
We can visualize the rotting procedure of oranges in the grid by having a look at the following illustration:
The illustration above shows that each orange acts as a root because the adjacent cells/oranges directly connected to the root orange are affected first. As we traverse through the grid from the top–left cell to the cells on the right, all the other connected oranges begin to rot. This process is similar to level-order traversal. To solve this problem, we use the queue data structure to keep track of the candidate nodes we need to visit during the process.
Here’s how to algorithm works:
Storing the rotten oranges and counting the fresh oranges:
We’ll iterate through the grid of oranges, and whenever we find a rotten orange (denoted by
2), we add it to therotten_queue. Otherwise, if we encounter any fresh orange (denoted by1), we increment the counter,fresh_oranges, by1.
Process of rotting oranges and counting minutes elapsed:
While there are rotten oranges in the
rotten_queue, we’ll process them in batches based on the current minute of the orange (this is denoted by the level of that orange in that graph).We’ll check the neighboring cells for each rotten orange in all four directions.
If a neighboring cell contains a fresh orange, we’ll mark it rotten by changing its value from
1to2, and thefresh_orangescount is decremented.The coordinates of the newly rotten orange will be added to the
rotten_queuefor the next minute’s processing.We’ll use the
minutesvariable to increment with each minute of processing.
After the “Process of rotting oranges and counting minutes elapsed” is completed, there are two possible outcomes:
If there are still fresh oranges left (
fresh_oranges > 0), it means not all oranges can be rotten, so-1is returned.Otherwise, the maximum of
0andminutes - 1is returned, indicating the time it took to rot all the oranges.
In the graph below, we have illustrated how the rotting process happens as we propagate through the layers, one level at a time:
1 of 14
2 of 14
3 of 14
4 of 14
5 of 14
6 of 14
7 of 14
8 of 14
9 of 14
10 of 14
11 of 14
12 of 14
13 of 14
14 of 14
Time complexity#
The time complexity of the above is
The
forloop iterates through the entire grid of sizeto identify the initial rotten oranges and count the fresh oranges. This initial loop has a time complexity of . Then, it enters a
whileloop that continues until there are no more fresh oranges left or until all possible rotten oranges have been processed. In the worst case, every fresh orange needs to be rotten, so this loop can iterate at mosttimes, which is . Inside the
whileloop, for each rotten orange, it checks its neighboring oranges. In the worst case, it can have 4 neighbors (up, down, left, right), and for each neighbor, it performs constant time operations (checks and updates). So, the operations inside the loop can be considered.
Overall, the time complexity of the code is while loop (in the worst case), which simplifies to
Space complexity#
The space complexity is
Rotting Oranges
Word Ladder