19. Rotten Oranges
Given an n x m grid, where each cell has the following values :Â
2Â -Â represents a rotten orange
1Â -Â represents a Fresh orange
0Â -Â represents an Empty Cell
Every minute, if a fresh orange is adjacent to a rotten orange in 4-direction ( upward, downwards, right, and left ) it becomes rotten.Â
Return the minimum number of minutes required such that none of the cells has a Fresh Orange. If it's not possible, return -1.
Example 1:
Input: grid = [ [2, 1, 1] , [0, 1, 1] , [1, 0, 1] ]
Output: -1
Explanation:Â Orange at (3,0) cannot be rotten.
Example 2:
Input: grid = [ [2,1,1] , [1,1,0] , [0,1,1] ]Â
Output:Â 4
Explanation:
Now Your Turn!
Pick the correct output for the given inputInput: grid = [[0,1,2],[0,1,2],[2,1,1]]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- Â Â 1 <= n, m <= 500
- Â Â grid[i][j] == 0 or 1 or 2