Statementâ–¼
Consider an
0 , which indicates an unoccupied cell.1 , representing a freshly picked orange.2 , 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:
m
= grid.length
n
= grid[i].length
1 ≤ m
,n
≤ 10 grid[i][j]
 isÂ0
,Â1
, orÂ2
.
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: