Walls and Gates
Given a m x n grid where -1 is a wall, 0 is a gate, and INF (2147483647) is an empty room. Fill each empty room with the distance to its nearest gate. If impossible to reach a gate, leave it as INF.

30:00

Walls and Gates
medium
Topics
Companies

Given a m x n grid where -1 is a wall, 0 is a gate, and INF (2147483647) is an empty room. Fill each empty room with the distance to its nearest gate. If impossible to reach a gate, leave it as INF.

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:
  • m==rooms.lengthm == \text{rooms.length}

  • n==rooms[i].lengthn == \text{rooms}[i].\text{length}

  • 1m,n2501 \leq m, n \leq 250

  • rooms[i][j]{1,0,INF}\text{rooms}[i][j] \in \{-1, 0, \text{INF}\} where INF =2311= 2^{31} - 1.

Input
arr ={"rooms":[[2147483647,-1,0,2147483647],[2147483647,2147483647,2147483647,-1],[2147483647,-1,2147483647,-1],[0,-1,2147483647,2147483647]]}

Initialize: Find all gates and add to queue

🧱
🚪
🧱
🧱
🧱
🚪
🧱
Gate (0)
Wall (-1)
Distance
In Queue
Variables
No variables to display
DepthFunction Call
Stack empty
0/11