← Visualizers
Graphs · Multi-source BFS LeetCode 994

Rotting Oranges / visualized

Every source goes into the queue before the first pop — then the levels come out right for free, with no per-source bookkeeping at all. The second half of the problem is the fresh counter, which is the only thing that tells finished apart from unreachable.

Execution

idle
Press Run to begin.
0 / 0
Speed

Java · running line

rotting now in the queue already rotten empty cell
O(m·n) time and space  ·  the answer is minutes - 1 because the sources themselves occupy level 1 and rot nothing; a fresh orange left at the end means it was never reachable, which is -1, not a larger number.