Rotting Orange Propagation
Problem statement
Given a rectangular grid, each cell is 0 for empty, 1 for a fresh orange, or 2 for a rotten orange.
For this exercise, assume each minute every orange rotten at the start of that minute infects its fresh neighbors directly above, below, left, and right. All infections in a minute happen simultaneously. Empty cells remain empty and do not transmit rot.
Return the smallest number of minutes until no fresh orange remains. Return -1 if some fresh orange can never rot, and 0 if the input has no fresh oranges.
The interview report identifies a BFS task similar to Rotten Oranges without describing its precise variation. These propagation rules define the authored practice version.
Function
minutesToRot(grid: int[][]) → intExamples
Example 1
grid = [[2,1],[1,1]]return = 2At minute 1 the two neighbors of the corner rot; at minute 2 the opposite corner rots.
Example 2
grid = [[2,1,0,1]]return = -1The empty cell blocks the route to the final fresh orange.
Example 3
grid = [[0,2]]return = 0No fresh orange is present, so no time is needed.
Constraints
- For this exercise, assume an empty grid is allowed. Otherwise there are
1through20rows and columns, and every row has the same length. - Every cell is one of
0,1, or2.