General
286. Walls and Gates
def wallsAndGates(self, rooms: List[List[int]]) -> None:
"""
Do not return anything, modify rooms in-place instead.
"""
if not rooms: return
m, n = len(rooms), len(rooms[0])
q = []
for i in range(m):
for j in range(n):
if rooms[i][j] == 0:
q.append((i, j))
step = 0
while q:
step += 1
size = len(q)
while size > 0:
i, j = q.pop(0)
for dy, dx in ((0, 1), (1, 0), (0, -1), (-1, 0)):
y, x = i + dy, j+ dx
if not (0 <= y < m and 0 <= x < n): continue
if rooms[y][x] == 2147483647:
rooms[y][x] = step
q.append((y, x))
size-=1317. Shortest Distance from All Buildings
909. Snakes and Ladders
Last updated