Home
Log in
DescriptionSubmissionsSolution
DescriptionSubmissionsSolution
Loading...

Log in to run or submit

grid =

Codey

Practise coding problems, test your solutions and track your progress.

Explore

  • Problems
  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

© 2026 Codey. Personal learning project.

Solution
Updated: 2026-02-23

Idea

Multi-source BFS from all initially rotten oranges.

Approach

Enqueue all rotten positions. Count fresh. BFS level by level (each level = 1 minute), rotting adjacent fresh oranges. At the end, if fresh remains, return -1.

Why it works

Rot spreads uniformly in waves; BFS levels model elapsed time exactly.

Code
Loading...
Complexity
Time: O(mn)
Space: O(mn)
Solution
Updated: 2026-02-23

Idea

Multi-source BFS from all initially rotten oranges.

Approach

Enqueue all rotten positions. Count fresh. BFS level by level (each level = 1 minute), rotting adjacent fresh oranges. At the end, if fresh remains, return -1.

Why it works

Rot spreads uniformly in waves; BFS levels model elapsed time exactly.

Code
Loading...
Complexity
Time: O(mn)
Space: O(mn)