-
Notifications
You must be signed in to change notification settings - Fork 0
/
151.py
125 lines (104 loc) · 2.65 KB
/
151.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
120
121
122
123
124
125
"""
Problem:
Given a 2-D matrix representing an image, a location of a pixel in the screen and a
color C, replace the color of the given pixel and all adjacent same colored pixels
with C.
For example, given the following matrix, and location pixel of (2, 2), and 'G' for
green:
B B W
W W W
W W W
B B B
Becomes
B B G
G G G
G G G
B B B
"""
from typing import List, Set, Tuple
from numpy import array
Matrix = List[List[int]]
Position = Tuple[int, int]
def generate_neighbours(position: Position, rows: int, cols: int) -> List[Position]:
i, j = position
valid_neighbours = []
neighbours = [
(i - 1, j - 1),
(i - 1, j),
(i - 1, j + 1),
(i, j + 1),
(i + 1, j + 1),
(i + 1, j),
(i + 1, j - 1),
(i, j - 1),
]
for neighbour in neighbours:
y, x = neighbour
if (0 <= x < cols) and (0 <= y < rows):
valid_neighbours.append(neighbour)
return valid_neighbours
def update_color_dfs_helper(
matrix: Matrix,
position: Position,
new_color: str,
prev_color: str,
visited: Set[Position],
rows: int,
cols: int,
) -> None:
i, j = position
matrix[i][j] = new_color
visited.add(position)
neighbours = generate_neighbours(position, rows, cols)
for neighbour in neighbours:
y, x = neighbour
if neighbour not in visited and matrix[y][x] == prev_color:
update_color_dfs_helper(
matrix, neighbour, new_color, prev_color, visited, rows, cols
)
def update_color(matrix: Matrix, position: Position, new_color: str) -> Matrix:
rows = len(matrix)
cols = len(matrix[0])
i, j = position
update_color_dfs_helper(
matrix, position, new_color, matrix[i][j], set(), rows, cols
)
return matrix
if __name__ == "__main__":
print("Initial Matrix:")
matrix = [
["B", "B", "W"],
["W", "W", "W"],
["W", "W", "W"],
["B", "B", "B"]
]
print(array(matrix))
print("Updated Matrix:")
print(array(update_color(matrix, (2, 2), "G")))
print()
print("Initial Matrix:")
matrix = [
["B", "B", "W"],
["W", "W", "W"],
["W", "W", "W"],
["B", "B", "B"]
]
print(array(matrix))
print("Updated Matrix:")
print(array(update_color(matrix, (3, 2), "G")))
print()
print("Initial Matrix:")
matrix = [
["B", "B", "W"],
["W", "W", "W"],
["W", "W", "W"],
["B", "B", "B"]
]
print(array(matrix))
print("Updated Matrix:")
print(array(update_color(matrix, (0, 0), "G")))
"""
SPECS:
TIME COMPLEXITY: O(n x m)
SPACE COMPLEXITY: O(n x m)
"""