Graph Theory37 sections · 1633 units
Open in Course

LeetCode 994 Rotting Oranges - Core Idea

Start from all sources

In single-source BFS, you push one cell to the queue and explore outward. In multi-source BFS, you push all starting cells to the queue before you begin.

Here, all initially rotten oranges are pushed to the queue at time 00. Then BFS spreads the rot in waves. After minute 11, all fresh oranges adjacent to any initial rotten orange become rotten. After minute 22, the rot spreads one more layer out.

The queue processes cells level by level. Each level corresponds to one minute. When the queue empties, you check if any fresh oranges remain. If yes, return 1-1. If no, return the elapsed minutes.