-
Notifications
You must be signed in to change notification settings - Fork 0
/
01-matrix.py
31 lines (25 loc) · 1.21 KB
/
01-matrix.py
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
28
29
30
31
# 542. 01 Matrix
class Solution:
def updateMatrix(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: List[List[int]]
"""
dist_matrix = [[10000 for _ in range(len(matrix[0]))] for _ in range(len(matrix))]
# from top-bottom/left-right
for row in range(len(matrix)):
for col in range(len(matrix[0])):
if matrix[row][col] == 0:
dist_matrix[row][col] = 0
if row > 0:
dist_matrix[row][col] = min(dist_matrix[row][col], dist_matrix[row - 1][col] + 1)
if col > 0:
dist_matrix[row][col] = min(dist_matrix[row][col], dist_matrix[row][col - 1] + 1)
# from bottom-top/right-left
for row in range(len(matrix) - 1, -1, -1):
for col in range(len(matrix[row]) - 1, -1, -1):
if row < len(matrix) - 1:
dist_matrix[row][col] = min(dist_matrix[row][col], dist_matrix[row + 1][col] + 1)
if col < len(matrix[row]) - 1:
dist_matrix[row][col] = min(dist_matrix[row][col], dist_matrix[row][col + 1] + 1)
return dist_matrix