Rotting Oranges - Problem

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 cell
  • 1 - A fresh orange
  • 2 - 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

example_1.py โ€” Standard Grid
$ Input: grid = [[2,1,1],[1,1,0],[0,1,1]]
โ€บ Output: 4
๐Ÿ’ก Note: The rot spreads from the single rotten orange at (0,0). After 1 minute: (0,1) and (1,0) rot. After 2 minutes: (0,2) and (1,1) rot. After 3 minutes: (2,1) rots. After 4 minutes: (1,2) and (2,2) rot. All fresh oranges are now rotten.
example_2.py โ€” Impossible Case
$ Input: grid = [[2,1,1],[0,1,1],[1,0,1]]
โ€บ Output: -1
๐Ÿ’ก Note: The orange at position (2,0) is isolated by empty cells and cannot be reached by the rot spreading from (0,0). Since not all fresh oranges can rot, we return -1.
example_3.py โ€” No Fresh Oranges
$ Input: grid = [[2]]
โ€บ Output: 0
๐Ÿ’ก Note: There are no fresh oranges to rot, so 0 minutes are needed.

Visualization

Tap to expand
๐ŸŠ Rotting Oranges VisualizationMulti-Source BFS ProcessRRottenFFreshEEmptyTime Progressiont=0t=1t=2t=4Algorithm Steps1. ๐Ÿ” Find all rotten oranges2. ๐Ÿ“Š Add them to BFS queue3. ๐ŸŒŠ Process level by level4. โฑ๏ธ Each level = 1 minute5. ๐ŸŽฏ Return max time or -1Time: O(mร—n) | Space: O(mร—n)๐ŸŸก Orange = Newly rotten this minute
Understanding the Visualization
1
Initial Contamination
Identify all initially contaminated oranges (value 2) and healthy oranges (value 1)
2
Wave Propagation
Each minute, contamination spreads to adjacent healthy oranges in all 4 directions
3
Simultaneous Spread
Multiple contamination sources spread simultaneously, creating overlapping infection waves
4
Completion Check
Process continues until no more healthy oranges can be infected
Key Takeaway
๐ŸŽฏ Key Insight: Multi-source BFS naturally handles simultaneous spreading from multiple points, ensuring we find the minimum time by processing all oranges at the same 'distance' (time) together.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(m ร— n)

Each cell is visited at most once when it gets rotten

n
2n
โœ“ Linear Growth
Space Complexity
O(m ร— n)

Queue can contain up to all cells in worst case

n
2n
โšก Linearithmic Space

Constraints

  • m == grid.length
  • n == grid[i].length
  • 1 โ‰ค m, n โ‰ค 10
  • grid[i][j] is 0, 1, or 2
Asked in
Amazon 42 Google 38 Meta 29 Microsoft 25 Apple 18
68.4K Views
High Frequency
~18 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen