Rotting Oranges

Write the code, see it run, pass the checks.

The problem

  • medium
  • Code

Write minutesToRot(grid). grid is an array of rows of numbers, all the same length: 0 is an empty cell, 1 a fresh orange and 2 a rotten one. Each minute, every fresh orange next to a rotten one, up, down, left or right, turns rotten. Return the number of minutes until no fresh orange is left, 0 if there's none to start with, or -1 if some orange never rots. You may change grid. For example, [[2, 1, 1], [1, 1, 0], [0, 1, 1]] takes 4 minutes. The checks include a 300 by 300 grid where the rot winds along one path for over 45,000 minutes.

Your code must pass

  • Counts the minutes until every orange is rotten
  • Spreads from several rotten oranges at once
  • Returns -1 when an orange can't be reached
  • Returns 0 when nothing is fresh
  • Is fast enough for a 300 by 300 grid