Rotting Oranges
You are given an m x n grid where each cell can have one of three values: 0 (empty), 1 (fresh orange), or 2 (rotten orange). Every minute, any fresh orange that is 4-directionally adjacent to a rotten orange becomes rotten. Return the minimum number of minutes until no cell has a fresh orange. If impossible, return -1.

30:00

Rotting Oranges
medium
Topics
Companies

You are given an m x n grid where each cell can have one of three values: 0 (empty), 1 (fresh orange), or 2 (rotten orange). Every minute, any fresh orange that is 4-directionally adjacent to a rotten orange becomes rotten. Return the minimum number of minutes until no cell has a fresh orange. If impossible, return -1.

Example 1:
Input: [[2,1,1],[1,1,0],[0,1,1]]
Output: 4
Constraints:
  • m==grid.lengthm == \text{grid.length}

  • n==grid[i].lengthn == \text{grid}[i].\text{length}

  • 1m,n101 \leq m, n \leq 10

  • grid[i][j]{0,1,2}\text{grid}[i][j] \in \{0, 1, 2\}

Input
arr =[[2,1,1],[1,1,0],[0,1,1]]
🥼
🍊
🍊
🍊
🍊
🍊
🍊
Minutes Elapsed

0

Found 1 rotten, 6 fresh oranges

Empty
Fresh
Rotten
Variables
VariableValue
fresh6
rotten1
DepthFunction Call
Stack empty
0/9