Home
Log in
DescriptionSubmissionsSolution
DescriptionSubmissionsSolution
Loading...

Log in to run or submit

rooms =

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

Compute distance to the nearest gate for every empty room.

Approach

Multi-source BFS starting from all gates (value 0). When expanding, only update cells with INF (2147483647) and set them to current + 1.

Why it works

BFS from multiple sources assigns the shortest distance to the closest gate.

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

Idea

Compute distance to the nearest gate for every empty room.

Approach

Multi-source BFS starting from all gates (value 0). When expanding, only update cells with INF (2147483647) and set them to current + 1.

Why it works

BFS from multiple sources assigns the shortest distance to the closest gate.

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