-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathTrapping Rain Water II.py
42 lines (32 loc) · 1.23 KB
/
Trapping Rain Water II.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
import heapq
class Solution:
def trapRainWater(self, heightMap: List[List[int]]) -> int:
ROW, COL = len(heightMap), len(heightMap[0])
pq = []
heapq.heapify(pq)
visited = {}
for row in range(ROW):
for col in range(COL):
if row == 0 or row == ROW-1 or col == 0 or col == COL-1:
heapq.heappush(pq, (heightMap[row][col],row,col))
visited[(row,col)] = True
def getnbr(row,col):
res = []
if row-1 >=0:
res.append((row-1,col))
if col-1 >=0:
res.append((row, col-1))
if row+1 < ROW:
res.append((row+1,col))
if col+1 < COL:
res.append((row, col+1))
return res
res = 0
while pq:
h, i, j = heapq.heappop(pq)
for dx, dy in getnbr(i,j):
if (dx,dy) not in visited:
res += max(0, h-heightMap[dx][dy])
heapq.heappush(pq, (max(h, heightMap[dx][dy]),dx,dy))
visited[(dx,dy)] = True
return res