-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlc1765.py
More file actions
67 lines (51 loc) · 10.9 KB
/
lc1765.py
File metadata and controls
67 lines (51 loc) · 10.9 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
66
import numpy as np
import time
class Solution:
# UNFINISHED FIX ME
def highestPeak(self, isWater: list[list[int]]) -> list[list[int]]:
m = len(isWater)
n = len(isWater[0])
water = []
border = []
print(time.time())
for i in range(m):
for j in range(n):
isWater[i][j] = 0 if isWater[i][j] == 1 else 1
if isWater[i][j] == 0:
water.append((i,j))
"""print(time.time())
for i,j in water:
neighbors = [(i-1,j),(i+1,j),(i,j-1),(i,j+1)]
has_land = False
for x,y in neighbors:
if 0 <= x < m and 0 <= y < n:
if isWater[x][y] == 1:
border.append((i,j))
break
"""
for br,bc in water:
q = [(br,bc)]
visited = set([(br,bc)])
#print(time.time(), isWater)
while q:
r,c = q.pop(0)
heights = []
neighbors = [(r-1,c),(r+1,c),(r,c-1),(r,c+1)]
for i,j in neighbors:
if 0 <= i < m and 0 <= j < n:
heights.append(isWater[i][j])
if (i,j) not in visited:
q.append((i,j))
visited.add((i,j))
min_height = min(heights)
if heights and isWater[r][c] != 0:
if isWater[r][c] != min_height+1:
isWater[r][c] = min_height + 1
# add r,c to the back of the queue
q.append((r,c))
return isWater
s = Solution()
#print(s.highestPeak([[1,0,0,0,0,0,0]]))
#print(s.highestPeak([[0],[0],[0],[0],[1],[0],[0],[1],[1]]))
#print(s.highestPeak([[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0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