-
Notifications
You must be signed in to change notification settings - Fork 0
/
max_connected.py
79 lines (71 loc) · 2.25 KB
/
max_connected.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
class Solution:
def maxConnected(self, grid):
maxCount = 0
for y in range(0, len(grid)):
for x in range(0, len(grid[y])):
count = self.__traverse(grid, x, y, {})
maxCount = max(count, maxCount)
return maxCount
def __traverse(self, grid, x, y, seen):
return self.__traverseHelper(grid, x, y, 0, seen)
def __traverseHelper(self, grid, x, y, count, seen):
key = "%s,%s" % (x, y)
if seen.get(key) is not None:
return 0
seen[key] = True
color = grid[y][x]
neighbors = self.__getNeighbors(grid, x, y)
sum_ = 1
for point in neighbors:
i = point[0]
j = point[1]
if grid[j][i] != color:
continue
seen[key] = True
# sum_ += self.__traverseHelper(grid, i, j, 1, seen)
sum_ += self.__traverseHelperIterative(grid, i, j, 1, seen)
return sum_
def __traverseHelperIterative(self, grid, x, y, count, seen):
color = grid[y][x]
sum_ = 0
stack = []
stack.append([x, y])
while len(stack) > 0:
p = stack.pop()
item_x = p[0]
item_y = p[1]
key = "%s,%s" % (item_x, item_y)
if seen.get(key) is not None:
continue
if grid[item_y][item_x] != color:
continue
seen[key] = True
sum_ = sum_ + 1
neighbors = self.__getNeighbors(grid, item_x, item_y)
for point in neighbors:
stack.append([point[0], point[1]])
return sum_
def __getNeighbors(self, grid, x, y):
coords = []
neighbors = [
[-1, 0],
[0, -1],
[1, 0],
[0, 1],
]
for n in neighbors:
coordX = x + n[0]
coordY = y + n[1]
if (
coordX < 0
or coordY < 0
or coordY >= len(grid)
or coordX >= len(grid[0])
):
continue
coords.append([coordX, coordY])
return coords
grid = [["r", "g", "b"],
["r", "r", "r"],
["g", "g", "r"]]
print(Solution().maxConnected(grid))