Rotting Oranges

Med
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start•← → to step•? all shortcuts

Solution Guide: Rotting Oranges

Approach

All rotten oranges act as Level 0 and spread rot outward together each minute. Use BFS with time per cell.

Complexity Analysis

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

Pattern

Multi-Source BFS

Why It Works

BFS frontier guarantees all oranges at the same distance from initial rotten oranges rot at the same minute; the final time is the maximum infection layer.

Updated Mar 2026