-
Notifications
You must be signed in to change notification settings - Fork 17
/
01-matrix.py
119 lines (95 loc) · 3.88 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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import unittest
from typing import List, Set, Tuple
class Solution:
def updateMatrix(self, matrix: List[List[int]]) -> List[List[int]]:
rows, cols = len(matrix), len(matrix[0]) if matrix else 0
dp = [
[rows * cols if matrix[row][col] else 0 for col in range(cols)]
for row in range(rows)
]
# Top-down, left-right
for row in range(rows):
for col in range(cols):
if col > 0:
dp[row][col] = min(dp[row][col], dp[row][col - 1] + 1)
if row > 0:
dp[row][col] = min(dp[row][col], dp[row - 1][col] + 1)
# Bottom-up, right-left
for row in reversed(range(rows)):
for col in reversed(range(cols)):
if col < cols - 1:
dp[row][col] = min(dp[row][col], dp[row][col + 1] + 1)
if row < rows - 1:
dp[row][col] = min(dp[row][col], dp[row + 1][col] + 1)
return dp
def updateMatrixBFS(self, matrix: List[List[int]]) -> List[List[int]]:
output = [[0 for _ in range(len(matrix[0]))] for _ in range(len(matrix))]
for y in range(len(matrix)):
for x in range(len(matrix[0])):
if matrix[y][x] == 0:
continue
stack = [(y, x)]
visited: Set[Tuple[int, int]] = set()
level = 1
while stack:
stack_old = stack
stack: List[Tuple[int, int]] = []
for new_y, new_x in stack_old:
visited.add((new_y, new_x))
for tmp_y, tmp_x in [
(new_y, new_x + 1),
(new_y, new_x - 1),
(new_y + 1, new_x),
(new_y - 1, new_x),
]:
if (
0 <= tmp_y < len(matrix)
and 0 <= tmp_x < len(matrix[0])
and not (tmp_y, tmp_x) in visited
):
if matrix[tmp_y][tmp_x] == 0:
output[y][x] = level
break
else:
stack.append((tmp_y, tmp_x))
else:
continue
break
else:
level += 1
continue
break
return output
class TestSolution(unittest.TestCase):
def setUp(self):
self.sol = Solution()
def test_one_element(self):
self.assertListEqual(self.sol.updateMatrix([[0]]), [[0]])
def test_two_element(self):
self.assertListEqual(self.sol.updateMatrix([[0], [0]]), [[0], [0]])
def test_two_element_2(self):
self.assertListEqual(self.sol.updateMatrix([[0, 0]]), [[0, 0]])
def test_two_element_3(self):
self.assertListEqual(self.sol.updateMatrix([[0, 1]]), [[0, 1]])
def test_case1(self):
self.assertListEqual(
self.sol.updateMatrix([[0, 1, 1], [1, 1, 1], [1, 1, 1]]),
[[0, 1, 2], [1, 2, 3], [2, 3, 4]],
)
def test_case2(self):
self.assertListEqual(
self.sol.updateMatrix([[0, 0, 0], [0, 1, 0], [1, 1, 1]]),
[[0, 0, 0], [0, 1, 0], [1, 2, 1]],
)
def test_case3(self):
self.assertListEqual(
self.sol.updateMatrix([[0, 0, 0], [0, 0, 0], [0, 0, 0]]),
[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
)
def test_case4(self):
self.assertListEqual(
self.sol.updateMatrix([[0, 0, 0], [0, 1, 0], [0, 0, 0]]),
[[0, 0, 0], [0, 1, 0], [0, 0, 0]],
)
if __name__ == "__main__":
unittest.main()