Limited time: AI code review, hints, mock interviews, whiteboard analysis, and all Pro features are unlocked. Enroll

Walls and Gates

Medium

Topics
GraphBFSMatrix

Fill each empty room with the distance to its nearest gate. -1 is a wall, 0 is a gate, and 2147483647 (INF) is an empty room. If a room cannot reach a gate, leave it as INF. Return the filled grid.

Example 1

Input:  rooms = [[2147483647,-1,0,2147483647],[2147483647,2147483647,2147483647,-1],[2147483647,-1,2147483647,-1],[0,-1,2147483647,2147483647]]
Output: [[3,-1,0,1],[2,2,1,-1],[1,-1,2,-1],[0,-1,3,4]]

Constraints

  • 1 <= m, n <= 250
  • Each cell is -1, 0, or 2147483647.
Run ⌘' · Submit ⌘⏎
SensAI SensAI
Beta
Listening...
Tap mic to stop voice mode

Unlock AI Features

One click to activate - no payment, no credit card. Just sign in and you're in.

AI code review and hints
SensAI chat assistant
AI mock interviews
Whiteboard analysis
100% free during early access