-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy path0542-01-matrix.py
91 lines (79 loc) · 3.46 KB
/
0542-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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# time complexity: O(m*n)
# space complexity: O(m*n)
from asyncio import Queue
from collections import deque
from typing import List
# BFS
class Solution:
def updateMatrix(self, matrix: List[List[int]]) -> List[List[int]]:
ROW = len(matrix)
COL = len(matrix[0])
queue = deque()
dist = [[float('inf') for _ in range(COL)] for _ in range(ROW)]
for r in range(ROW):
for c in range(COL):
if matrix[r][c] == 0:
dist[r][c] = 0
queue.append((r, c))
while queue:
currR, currC = queue.popleft()
for dR, dC in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
nextR = currR + dR
nextC = currC + dC
if 0 <= nextR < ROW and 0 <= nextC < COL:
if dist[nextR][nextC] > dist[currR][currC] + 1:
dist[nextR][nextC] = dist[currR][currC] + 1
queue.append((nextR, nextC))
return dist
# time complexity: O(m*n)
# space complexity: O(1)
class Solution:
def updateMatrix(self, matrix: List[List[int]]) -> List[List[int]]:
ROW = len(matrix)
COL = len(matrix[0])
for r in range(ROW):
for c in range(COL):
if matrix[r][c] > 0:
above = matrix[r-1][c] if r > 0 else float('inf')
left = matrix[r][c-1] if c > 0 else float('inf')
matrix[r][c] = min(above, left) + 1
for r in range(ROW - 1, -1, -1):
for c in range(COL - 1, -1, -1):
if matrix[r][c] > 0:
below = matrix[r+1][c] if r < ROW - 1 else float('inf')
right = matrix[r][c+1] if c < COL - 1 else float('inf')
minDistance = min(below, right) + 1
matrix[r][c] = min(matrix[r][c], minDistance)
return matrix
# time complexity: O(m*n)
# space complexity: O(m*n)
class Solution:
def updateMatrix(self, mat: List[List[int]]) -> List[List[int]]:
dp = [row[:] for row in mat]
m, n = len(dp), len(dp[0])
for row in range(m):
for col in range(n):
minNeighbor = float('inf')
if dp[row][col] != 0:
if row > 0:
minNeighbor = min(minNeighbor, dp[row - 1][col])
if col > 0:
minNeighbor = min(minNeighbor, dp[row][col - 1])
dp[row][col] = minNeighbor + 1
for row in range(m - 1, -1, -1):
for col in range(n - 1, -1, -1):
minNeighbor = float('inf')
if dp[row][col] != 0:
if row < m - 1:
minNeighbor = min(minNeighbor, dp[row + 1][col])
if col < n - 1:
minNeighbor = min(minNeighbor, dp[row][col + 1])
dp[row][col] = min(dp[row][col], minNeighbor + 1)
return dp
mat = [[0, 0, 0], [0, 1, 0], [0, 0, 0]]
print(Solution().updateMatrix(mat))
mat = [[0, 0, 0], [0, 1, 0], [1, 1, 1]]
print(Solution().updateMatrix(mat))
mat = [[1, 0, 1, 1, 0, 0, 1, 0, 0, 1], [0, 1, 1, 0, 1, 0, 1, 0, 1, 1], [0, 0, 1, 0, 1, 0, 0, 1, 0, 0], [1, 0, 1, 0, 1, 1, 1, 1, 1, 1], [0, 1, 0, 1, 1, 0, 0, 0, 0, 1], [
0, 0, 1, 0, 1, 1, 1, 0, 1, 0], [0, 1, 0, 1, 0, 1, 0, 0, 1, 1], [1, 0, 0, 0, 1, 1, 1, 1, 0, 1], [1, 1, 1, 1, 1, 1, 1, 0, 1, 0], [1, 1, 1, 1, 0, 1, 0, 0, 1, 1]]
print(Solution().updateMatrix(mat))