Skip to content

Commit 2446a73

Browse files
committed
Sync LeetCode submission Runtime - 235 ms (11.74%), Memory - 21.3 MB (32.28%)
1 parent 42cf38f commit 2446a73

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

0407-trapping-rain-water-ii/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<p>Given an <code>m x n</code> integer matrix <code>heightMap</code> representing the height of each unit cell in a 2D elevation map, return <em>the volume of water it can trap after raining</em>.</p>
2+
3+
<p>&nbsp;</p>
4+
<p><strong class="example">Example 1:</strong></p>
5+
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/08/trap1-3d.jpg" style="width: 361px; height: 321px;" />
6+
<pre>
7+
<strong>Input:</strong> heightMap = [[1,4,3,1,3,2],[3,2,1,3,2,4],[2,3,3,2,3,1]]
8+
<strong>Output:</strong> 4
9+
<strong>Explanation:</strong> After the rain, water is trapped between the blocks.
10+
We have two small ponds 1 and 3 units trapped.
11+
The total volume of water trapped is 4.
12+
</pre>
13+
14+
<p><strong class="example">Example 2:</strong></p>
15+
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/08/trap2-3d.jpg" style="width: 401px; height: 321px;" />
16+
<pre>
17+
<strong>Input:</strong> heightMap = [[3,3,3,3,3],[3,2,2,2,3],[3,2,1,2,3],[3,2,2,2,3],[3,3,3,3,3]]
18+
<strong>Output:</strong> 10
19+
</pre>
20+
21+
<p>&nbsp;</p>
22+
<p><strong>Constraints:</strong></p>
23+
24+
<ul>
25+
<li><code>m == heightMap.length</code></li>
26+
<li><code>n == heightMap[i].length</code></li>
27+
<li><code>1 &lt;= m, n &lt;= 200</code></li>
28+
<li><code>0 &lt;= heightMap[i][j] &lt;= 2 * 10<sup>4</sup></code></li>
29+
</ul>
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Approach: BFS + Priority Queue
2+
3+
# m = no. of rows, n = no. of cols
4+
# Time: O(m * n * log(mn))
5+
# Space: O(m * n)
6+
7+
import heapq
8+
9+
class Solution:
10+
# Class to store the height and coordinates of a cell in the grid
11+
class Cell:
12+
def __init__(self, height, row, col):
13+
self.height = height
14+
self.row = row
15+
self.col = col
16+
17+
# Comparison method for the priority queue (min-heap)
18+
def __lt__(self, other):
19+
return self.height < other.height
20+
21+
def _is_valid_cell(self, row, col, num_rows, num_cols):
22+
return 0 <= row < num_rows and 0 <= col < num_cols
23+
24+
def trapRainWater(self, heightMap: List[List[int]]) -> int:
25+
# Direction arrays
26+
d_row = [0, 0, -1, 1]
27+
d_col = [-1, 1, 0, 0]
28+
29+
num_rows = len(heightMap)
30+
num_cols = len(heightMap[0])
31+
32+
visited = [[False] * num_cols for _ in range(num_rows)]
33+
34+
# Priority queue (min-heap) to process boundary cells in increasing height order
35+
boundary = []
36+
37+
# Add the first and last column cells to the boundary and mark them as visited
38+
for i in range(num_rows):
39+
heapq.heappush(boundary, self.Cell(heightMap[i][0], i, 0))
40+
heapq.heappush(boundary, self.Cell(heightMap[i][num_cols - 1], i, num_cols - 1))
41+
visited[i][0] = visited[i][num_cols - 1] = True
42+
43+
# Add the first and last row cells to the boundary and mark them as visited
44+
for i in range(num_cols):
45+
heapq.heappush(boundary, self.Cell(heightMap[0][i], 0, i))
46+
heapq.heappush(boundary, self.Cell(heightMap[num_rows - 1][i], num_rows - 1, i))
47+
visited[0][i] = visited[num_rows - 1][i] = True
48+
49+
# Result
50+
total_water_volume = 0
51+
52+
# Process cells in the boundary (min-heap will always pop the smallest height)
53+
while boundary:
54+
# Pop the cell with the smallest height from the boundary
55+
current_cell = heapq.heappop(boundary)
56+
57+
current_row = current_cell.row
58+
current_col = current_cell.col
59+
min_boundary_height = current_cell.height
60+
61+
# Explore all 4 neighboring cells
62+
for direction in range(4):
63+
neighbor_row = current_row + d_row[direction]
64+
neighbor_col = current_col + d_col[direction]
65+
66+
if (self._is_valid_cell(neighbor_row, neighbor_col, num_rows, num_cols) and not visited[neighbor_row][neighbor_col]):
67+
neighbor_height = heightMap[neighbor_row][neighbor_col]
68+
69+
# If the neighbor's height is less than the current boundary height, water can be trapped
70+
if neighbor_height < min_boundary_height:
71+
total_water_volume += min_boundary_height - neighbor_height
72+
73+
# Push the neighbor into the boundary with updated height (to prevent water leakage)
74+
heapq.heappush(boundary, self.Cell(max(neighbor_height, min_boundary_height), neighbor_row, neighbor_col))
75+
visited[neighbor_row][neighbor_col] = True
76+
77+
return total_water_volume
78+
79+
80+
81+
82+
83+

0 commit comments

Comments
 (0)