Rotting Oranges

Try to solve the Rotting Oranges problem.

Statement

Consider an m×nm \times n grid containing cells with three potential values:

  • 00, which indicates an unoccupied cell.

  • 11, representing a freshly picked orange.

  • 22, indicating a rotten orange.

Any fresh orange that is 4–directionally adjacent to a rotten orange will also turn rotten with each passing minute.

Your task is to determine the minimum time required for all cells to have rotten oranges. In case, this objective cannot be achieved, return −1-1.

Constraints:

  • m == grid.length

  • n == grid[i].length

  • 11 ≤\leq m, n ≤\leq 1010

  • grid[i][j] is 0, 1, or 2.

Examples

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.