medium
matrix
graph
bfs
queue
A grid uses 0 for empty, 1 for a fresh orange, and 2 for a rotten orange. Each minute, rot spreads horizontally and vertically. Return the minimum minutes until no fresh orange remains, or -1 if impossible.
Examples
Example 1
Input: grid = [[2,1,1],[1,1,0],[0,1,1]]
Output: 4
Example 2
Input: grid = [[2,1,1],[0,1,1],[1,0,1]]
Output: -1
Example 3
Input: grid = [[0,2]]
Output: 0
🔒 5 hidden
Running will execute all 8 cases, including 5 hidden ones.