Skip to contentKEEP THE CRAFTTHE SANCTUARY

Rotting Oranges

Find how many minutes it takes for rot to reach every fresh orange.

The problem

Implement Solution.orangesRotting(grid). Each cell is 0 (empty), 1 (a fresh orange) or 2 (a rotten orange). Every minute, each rotten orange makes the fresh oranges directly above, below, left and right of it rotten. Return the number of minutes until no fresh orange is left, or -1 if some fresh orange can never rot. If there are no fresh oranges at the start, return 0. The grid has 1 to 10 rows and 1 to 10 columns. You may change grid while you work.

Starter code

class Solution:
    def orangesRotting(self, grid):
        pass

Public examples

  1. grid = [[2, 1, 1], [1, 1, 0], [0, 1, 1]]gives4
  2. grid = [[2, 1]]gives1
  3. grid = [[1]]gives-1

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.