Skip to content

Commit

Permalink
Create 10. Trapping Rain Water II.py
Browse files Browse the repository at this point in the history
  • Loading branch information
SamirPaulb committed Jan 10, 2023
1 parent 9009943 commit 61546d4
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions 08_Heap/10. Trapping Rain Water II.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# https://leetcode.com/problems/trapping-rain-water-ii/
# https://youtu.be/QvQiQcLCQ4Y

import heapq
class Solution:
def trapRainWater(self, heightMap):
minHeap = []
ROW, COL = len(heightMap), len(heightMap[0])
visited = [[False]*COL for _ in range(ROW)]

for i in range(ROW):
for j in range(COL):
if i in (0, ROW-1) or j in (0, COL-1):
heapq.heappush(minHeap, (heightMap[i][j], i, j))
visited[i][j] = True

res = 0
minBdH = 0 # Minimum Boundary Height
while minHeap:
h, i, j = heapq.heappop(minHeap)
minBdH = max(minBdH, h)
for dx, dy in ((1,0),(-1,0),(0,1),(0,-1)):
r, c = i+dx, j+dy
if 0<=r<ROW and 0<=c<COL and not visited[r][c]:
visited[r][c] = True
heapq.heappush(minHeap, (heightMap[r][c], r, c))
if heightMap[r][c] < minBdH:
res += minBdH - heightMap[r][c]

return res



# Time: O(m*n * log(m*n))
# Space: O(m*n)

0 comments on commit 61546d4

Please sign in to comment.