Imagine you're managing a fruit storage warehouse where oranges are stored in a rectangular grid. The warehouse has been contaminated, and some oranges have started rotting! ๐
You are given an m ร n grid where each cell can contain:
0- An empty cell1- A fresh orange2- A rotten orange
The rot spreads rapidly! Every minute, any fresh orange that is 4-directionally adjacent (up, down, left, right) to a rotten orange becomes rotten.
Your mission: Determine the minimum number of minutes that must elapse until no cell has a fresh orange. If it's impossible for all fresh oranges to rot (some are completely isolated), return -1.
This is a classic multi-source BFS problem - perfect for simulating simultaneous spreading processes!
Input & Output
Visualization
Time & Space Complexity
Each cell is visited at most once when it gets rotten
Queue can contain up to all cells in worst case
Constraints
- m == grid.length
- n == grid[i].length
- 1 โค m, n โค 10
- grid[i][j] is 0, 1, or 2