Walls and Gates

Medium · Queue & Deque · about 20 minutes

Asked at Meta, DoorDash, Google, Amazon, Uber, Grab, Swiggy.

You are given an m × n grid of rooms: -1 is a wall, 0 is a gate and 2147483647 (INF) is an empty room. Fill each empty room with the distance (in 4-directional steps) to its nearest gate; leave it INF if no gate is reachable. Modify the grid in place and return it.

Related guides