-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy path542-yongjoonseo.py
More file actions
27 lines (25 loc) · 905 Bytes
/
542-yongjoonseo.py
File metadata and controls
27 lines (25 loc) · 905 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from collections import deque
class Solution:
def updateMatrix(self, matrix: List[List[int]]) -> List[List[int]]:
n, m = len(matrix), len(matrix[0])
visited = [[0] * m for i in range(n)]
dy = [0, 1, 0, -1]
dx = [1, 0, -1, 0]
q = deque()
for i in range(n):
for j in range(m):
if not matrix[i][j]:
q.append((i, j))
visited[i][j] = 1
cnt = 0
while q:
cnt += 1
for _ in range(len(q)):
y, x = q.popleft()
for i in range(4):
ny, nx = y + dy[i], x + dx[i]
if 0 <= ny < n and 0 <= nx < m and not visited[ny][nx]:
visited[ny][nx] = 1
matrix[ny][nx] = cnt
q.append((ny, nx))
return matrix