-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathmulti-source-flood-fill.py
More file actions
65 lines (62 loc) · 2.01 KB
/
multi-source-flood-fill.py
File metadata and controls
65 lines (62 loc) · 2.01 KB
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
# Time: O(n * m)
# Space: O(n * m)
# bfs, flood fill
class Solution(object):
def colorGrid(self, n, m, sources):
"""
:type n: int
:type m: int
:type sources: List[List[int]]
:rtype: List[List[int]]
"""
DIRECTIONS = ((1, 0), (0, 1), (-1, 0), (0, -1))
result = [[0]*m for _ in xrange(n)]
q = []
for r, c, color in sources:
result[r][c] = color
q.append((r, c))
while q:
new_q = []
for r, c in q:
for dr, dc in DIRECTIONS:
nr, nc = r+dr, c+dc
if not (0 <= nr < n and 0 <= nc < m):
continue
if result[nr][nc] == 0:
result[nr][nc] = -result[r][c]
new_q.append((nr, nc))
elif result[nr][nc] < 0:
result[nr][nc] = min(result[nr][nc], -result[r][c])
for nr, nc in new_q:
result[nr][nc] = -result[nr][nc]
q = new_q
return result
# Time: O(slogs + n * m) = O((n * m) * log(n * m))
# Space: O(n * m)
# sort, bfs, flood fill
class Solution2(object):
def colorGrid(self, n, m, sources):
"""
:type n: int
:type m: int
:type sources: List[List[int]]
:rtype: List[List[int]]
"""
DIRECTIONS = ((1, 0), (0, 1), (-1, 0), (0, -1))
sources.sort(key=lambda x: -x[2])
result = [[0]*m for _ in xrange(n)]
q = []
for r, c, color in sources:
result[r][c] = color
q.append((r, c))
while q:
new_q = []
for r, c in q:
for dr, dc in DIRECTIONS:
nr, nc = r+dr, c+dc
if not (0 <= nr < n and 0 <= nc < m and result[nr][nc] == 0):
continue
result[nr][nc] = result[r][c]
new_q.append((nr, nc))
q = new_q
return result