forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap-of-highest-peak.py
63 lines (58 loc) · 1.93 KB
/
map-of-highest-peak.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
# Time: O(m * n)
# Space: O(m * n)
class Solution(object):
def highestPeak(self, isWater):
"""
:type isWater: List[List[int]]
:rtype: List[List[int]]
"""
directions = [(1, 0), (-1, 0), (0, 1), (0, -1)]
q = []
for r, row in enumerate(isWater):
for c, cell in enumerate(row):
row[c] -= 1
if not cell:
continue
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 < len(isWater) and
0 <= nc < len(isWater[0]) and
isWater[nr][nc] == -1):
continue
isWater[nr][nc] = isWater[r][c]+1
q.append((nr, nc))
q = new_q
return isWater
# Time: O(m * n)
# Space: O(m * n)
class Solution2(object):
def highestPeak(self, isWater):
"""
:type isWater: List[List[int]]
:rtype: List[List[int]]
"""
directions = [(1, 0), (-1, 0), (0, 1), (0, -1)]
q, heights = [], [[-1]*len(isWater[0]) for _ in xrange(len(isWater))]
for r, row in enumerate(isWater):
for c, cell in enumerate(row):
if not cell:
continue
heights[r][c] = 0
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 < len(isWater) and
0 <= nc < len(isWater[0]) and
heights[nr][nc] == -1):
continue
heights[nr][nc] = heights[r][c]+1
q.append((nr, nc))
q = new_q
return heights