PIXELBANKv9.1.0
Menu

In a grid, each cell can be empty (0), a fresh orange (1), or a rotten orange (2). Every minute, fresh oranges adjacent (4-directionally) to rotten ones become rotten.

Return the minimum minutes until no fresh orange remains, or -1 if impossible.

Input: grid rows separated by newlines, each row comma-separated.

Example:

Input:
2,1,1
1,1,0
0,1,1
Output:
4
Reasoning:
  • Initially, the grid is: 2, 1, 1 1, 1, 0 0, 1, 1 with one rotten orange (2) and five fresh oranges (1)
  • After the first minute, the fresh oranges adjacent to the rotten one become rotten: 2, 2, 2 1, 1, 0 0, 1, 1
  • In the next two minutes, the remaining fresh oranges become rotten: 2, 2, 2 2, 2, 0 0, 2, 2 and then all oranges are rotten after a total of 2+1+1=42 + 1 + 1 = 4 minutes, but since the first minute has already passed, it takes a total of 4 minutes
  • The final output is the minimum minutes until no fresh orange remains, which is 44

Constraints:

  • 1 <= rows, cols <= 10
  • grid[i][j] is 0, 1, or 2
solution.py

Test Results

0/0
Run code to see test results.
Rotten Oranges - Medium | PixelBank