my dsa gallery
GraphMulti-source BFS

Rotting Oranges

Problem link: https://leetcode.com/problems/rotting-oranges/

Problem Statement

You are given an m x n grid where each cell can have one of three values: 0 → empty cell 1 → fresh orange 2 → rotten orange

Every minute, any fresh orange adjacent (4-directionally) to a rotten orange becomes rotten.

Return the minimum number of minutes required to rot all oranges. If impossible, return -1.

Input

A 2D grid of size m x n containing values 0, 1, or 2.

Output

Return minimum minutes required to rot all oranges, or -1 if not possible.

Example

Input: grid = [[2,1,1],[1,1,0],[0,1,1]]

Output: 4

Explanation

All rotten oranges spread simultaneously. Each BFS level represents 1 minute.

Minute 0: Initial rotten oranges Minute 1+: Spread to adjacent fresh oranges

Final answer = total BFS levels required

Step 1 of 3- Minute 0
Initial state
Loading...
Use arrow keys to navigate

Brute Force

Intuition

Simulate minute by minute. For each minute, scan the entire grid and convert fresh oranges adjacent to rotten ones. Repeat until no changes occur.

Approach

  1. Loop until no changes occur
  2. Traverse full grid
  3. For each rotten orange, mark adjacent fresh oranges
  4. Apply updates
  5. Count minutes
Step 1 of 3- Step 1
Scan grid
Loading...
Use arrow keys to navigate

Dry Run

Each iteration processes entire grid → inefficient. Time increases even if few oranges are changing.

Code (Java)

class Solution {
    public int orangesRotting(int[][] grid) {
        int m = grid.length, n = grid[0].length;
        int minutes = 0;
        boolean changed;

        do {
            changed = false;
            int[][] copy = new int[m][n];

            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    copy[i][j] = grid[i][j];
                }
            }

            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    if (grid[i][j] == 2) {
                        int[][] dir = {{1,0},{-1,0},{0,1},{0,-1}};
                        for (int[] d : dir) {
                            int ni = i + d[0], nj = j + d[1];
                            if (ni>=0 && nj>=0 && ni<m && nj<n && grid[ni][nj] == 1) {
                                copy[ni][nj] = 2;
                                changed = true;
                            }
                        }
                    }
                }
            }

            if (changed) minutes++;
            grid = copy;

        } while (changed);

        for (int[] row : grid) {
            for (int cell : row) {
                if (cell == 1) return -1;
            }
        }

        return minutes;
    }
}
Time: O((m*n)^2)Space: O(m*n)

Complexity Analysis

Each iteration scans full grid, and worst-case we do this O(m*n) times.

Optimal Solution

Intuition

All rotten oranges act as starting points. Spread happens simultaneously → BFS level order traversal.

Approach

  1. Add all rotten oranges to queue
  2. Count fresh oranges
  3. Perform BFS
  4. For each level:
    • Rot adjacent fresh oranges
    • Decrease fresh count
  5. Increment time per level
  6. If fresh remains → return -1
Step 1 of 3- Step 1
Initialize queue
Loading...
Use arrow keys to navigate

Dry Run

Queue initially has all rotten oranges. Each BFS level spreads rot. Time increments after each level.

Step 1 of 3- Minute 0
Initial queue
Loading...
Use arrow keys to navigate

Code (Java)

Time: O(m*n)Space: O(m*n)

Complexity Analysis

Each cell is processed once in BFS.

Quick Revision (Brute Force)

  • Simulate minute-by-minute
  • Scan full grid repeatedly
  • High time complexity

Quick Revision (Optimal)

  • Multi-source BFS
  • Each level = 1 minute
  • Process all rotten together

Study Photos

Upload screenshots/notes for this specific problem.

Drag & drop or click to select
Drop an image anywhere in this box to upload.
No photos uploaded till now.

Comments

Stored in D1. Login required.
  • Good video for graph : https://www.youtube.com/watch?v=xGiH8gN9x2g
    2026-04-12T14:36:08.800Z
  • Nice question
    2026-04-12T14:35:45.702Z