Skip to content

Commit 7df8915

Browse files
authored
Create 778-Swim-in-Rising-Water.py
1 parent cadad75 commit 7df8915

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

778-Swim-in-Rising-Water.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def swimInWater(self, grid: List[List[int]]) -> int:
3+
N = len(grid)
4+
visit = set()
5+
minH = [[grid[0][0], 0, 0]] # (time/max-height, r, c)
6+
directions = [[0, 1], [0, -1], [1, 0], [-1 ,0]]
7+
8+
visit.add((0, 0))
9+
while minH:
10+
t, r, c = heapq.heappop(minH)
11+
if r == N - 1 and c == N - 1:
12+
return t
13+
for dr, dc in directions:
14+
neiR, neiC = r + dr, c + dc
15+
if (neiR < 0 or neiC < 0 or
16+
neiR == N or neiC == N or
17+
(neiR, neiC) in visit):
18+
continue
19+
visit.add((neiR, neiC))
20+
heapq.heappush(minH, [max(t, grid[neiR][neiC]), neiR, neiC])

0 commit comments

Comments
 (0)