-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path749.py
157 lines (143 loc) · 6.9 KB
/
749.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
__________________________________________________________________________________________________
sample 124 ms submission
class Solution: #eat my ass
def containVirus(self, grid: List[List[int]]) -> int:
N, M, out = len(grid), len(grid[0]), 0
regions = self.initialize(grid)
while True:
threats, max_idx, max_threat, wall_count = self.find_threats(regions, grid)
if max_threat == 0: break
out += wall_count
regions.pop(max_idx)
threats.pop(max_idx)
regions, grid = self.advance_virus(regions, grid, threats)
return out\
def initialize(self, grid):
regions, seen = [], set()
for row in range(len(grid)):
for col in range(len(grid[0])):
if grid[row][col] == 0 or (row,col) in seen: continue
stack, new_region = [(row,col)], []
seen.add((row,col))
while stack:
i0, j0 = stack.pop()
edge = False
for i, j in [(i0+1,j0),(i0-1,j0),(i0,j0+1),(i0,j0-1)]:
if (i,j) not in seen and 0 <= i < len(grid) and 0 <= j < len(grid[0]):
if grid[i][j] == 1:
seen.add((i,j))
stack.append((i,j))
else: edge = True
if edge: new_region.append((i0,j0))
regions.append(new_region)
return regions
def find_threats(self, regions, grid):
threats, max_idx, max_threat, wall_count = [], 0, 0, 0
for idx, region in enumerate(regions):
curr_threat, curr_walls = set(), 0
for i0, j0 in region:
for i, j in [(i0+1,j0),(i0-1,j0),(i0,j0+1),(i0,j0-1)]:
if 0 <= i < len(grid) and 0 <= j < len(grid[0]) and grid[i][j] == 0:
curr_threat.add((i,j))
curr_walls += 1
if len(curr_threat) > max_threat: max_idx, max_threat, wall_count = idx, len(curr_threat), curr_walls
threats.append(curr_threat)
return threats, max_idx, max_threat, wall_count
def advance_virus(self, regions, grid, threats):
def union_join(i, j):
leader_i, leader_j = find(i), find(j)
remap[leader_j] = remap[j] = remap[i] = leader_i
def find(i):
while remap[i] != i: i = remap[i]
return i
remap, converted = {idx: idx for idx in range(len(regions))}, {}
for idx, region in enumerate(regions):
updated_region = []
for i0, j0 in threats[idx]:
if (i0,j0) in converted: union_join(converted[(i0,j0)], idx)
else:
converted[(i0,j0)], grid[i0][j0] = idx, 1
updated_region.append((i0,j0))
for i, j in [(i0+1,j0),(i0-1,j0),(i0,j0+1),(i0,j0-1)]:
if (i,j) in converted and converted[(i,j)] != idx:
union_join(converted[(i,j)], idx)
regions[idx] = updated_region
new_regions = []
for i in range(len(regions)):
if remap[i] != i:
leader_i = find(i)
regions[leader_i].extend(regions[i])
else: new_regions.append(regions[i])
return new_regions, grid
__________________________________________________________________________________________________
sample 124 ms submission
class Solution: #eat my ass
def containVirus(self, grid: List[List[int]]) -> int:
N, M, out = len(grid), len(grid[0]), 0
regions = self.initialize(grid)
while True:
threats, max_idx, max_threat, wall_count = self.find_threats(regions, grid)
if max_threat == 0: break
out += wall_count
regions.pop(max_idx)
threats.pop(max_idx)
regions, grid = self.advance_virus(regions, grid, threats)
return out\
def initialize(self, grid):
regions, seen = [], set()
for row in range(len(grid)):
for col in range(len(grid[0])):
if grid[row][col] == 0 or (row,col) in seen: continue
stack, new_region = [(row,col)], []
seen.add((row,col))
while stack:
i0, j0 = stack.pop()
edge = False
for i, j in [(i0+1,j0),(i0-1,j0),(i0,j0+1),(i0,j0-1)]:
if (i,j) not in seen and 0 <= i < len(grid) and 0 <= j < len(grid[0]):
if grid[i][j] == 1:
seen.add((i,j))
stack.append((i,j))
else: edge = True
if edge: new_region.append((i0,j0))
regions.append(new_region)
return regions
def find_threats(self, regions, grid):
threats, max_idx, max_threat, wall_count = [], 0, 0, 0
for idx, region in enumerate(regions):
curr_threat, curr_walls = set(), 0
for i0, j0 in region:
for i, j in [(i0+1,j0),(i0-1,j0),(i0,j0+1),(i0,j0-1)]:
if 0 <= i < len(grid) and 0 <= j < len(grid[0]) and grid[i][j] == 0:
curr_threat.add((i,j))
curr_walls += 1
if len(curr_threat) > max_threat: max_idx, max_threat, wall_count = idx, len(curr_threat), curr_walls
threats.append(curr_threat)
return threats, max_idx, max_threat, wall_count
def advance_virus(self, regions, grid, threats):
def union_join(i, j):
leader_i, leader_j = find(i), find(j)
remap[leader_j] = remap[j] = remap[i] = leader_i
def find(i):
while remap[i] != i: i = remap[i]
return i
remap, converted = {idx: idx for idx in range(len(regions))}, {}
for idx, region in enumerate(regions):
updated_region = []
for i0, j0 in threats[idx]:
if (i0,j0) in converted: union_join(converted[(i0,j0)], idx)
else:
converted[(i0,j0)], grid[i0][j0] = idx, 1
updated_region.append((i0,j0))
for i, j in [(i0+1,j0),(i0-1,j0),(i0,j0+1),(i0,j0-1)]:
if (i,j) in converted and converted[(i,j)] != idx:
union_join(converted[(i,j)], idx)
regions[idx] = updated_region
new_regions = []
for i in range(len(regions)):
if remap[i] != i:
leader_i = find(i)
regions[leader_i].extend(regions[i])
else: new_regions.append(regions[i])
return new_regions, grid
__________________________________________________________________________________________________