Tap here to switch tabs
Problem
Submissions

Problem: Walls and Gates

med
30 min
Try to solve the Walls and Gates problem.

Statement

You are given a rectangular grid rooms of size m by n where each cell represents one of the following:
a wall with value −1-1
a gate with value 00
an empty room with value 231−12^{31} - 1

Update rooms in place so that each empty room contains the distance to its nearest gate, measured as the minimum number of moves between adjacent cells sharing a side. If an empty room cannot reach any gate, its value must remain 231−12^{31} - 1. Walls and gates must remain unchanged.

Note: Modify rooms in place and do not return anything.

Constraints:

  • m == rooms.length

  • n == rooms[i].length

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

  • rooms[i][j] is −1-1, 00, or 231−12^{31} - 1

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Walls and Gates

med
30 min
Try to solve the Walls and Gates problem.

Statement

You are given a rectangular grid rooms of size m by n where each cell represents one of the following:
a wall with value −1-1
a gate with value 00
an empty room with value 231−12^{31} - 1

Update rooms in place so that each empty room contains the distance to its nearest gate, measured as the minimum number of moves between adjacent cells sharing a side. If an empty room cannot reach any gate, its value must remain 231−12^{31} - 1. Walls and gates must remain unchanged.

Note: Modify rooms in place and do not return anything.

Constraints:

  • m == rooms.length

  • n == rooms[i].length

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

  • rooms[i][j] is −1-1, 00, or 231−12^{31} - 1