Skip to contentKEEP THE CRAFTTHE SANCTUARY

Walls and Gates

Fill every empty room with its distance to the nearest gate.

The problem

Implement Solution.wallsAndGates(rooms). rooms is a grid where -1 is a wall, 0 is a gate and 2147483647 is an empty room. You can move up, down, left or right, but not through walls. Replace each empty room with the number of steps to its nearest gate. A room that cannot reach any gate keeps 2147483647. Change rooms in place and also return it. The grid has 1 to 100 rows and 1 to 100 columns. If the grid is empty, return it unchanged.

Starter code

class Solution:
    def wallsAndGates(self, rooms):
        pass

Public examples

  1. rooms = [[2147483647, -1, 0, 2147483647], [2147483647, 2147483647, 2147483647, -1], [2147483647, -1, 2147483647, -1], [0, -1, 2147483647, 2147483647]]gives[[3, -1, 0, 1], [2, 2, 1, -1], [1, -1, 2, -1], [0, -1, 3, 4]]
  2. rooms = [[0, 2147483647]]gives[[0, 1]]
  3. rooms = [[2147483647, 2147483647, 0]]gives[[2, 1, 0]]

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.