-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
407. Trapping Rain Water II #1180
Comments
The "Trapping Rain Water II" problem is a challenging computational problem that requires us to compute the volume of water trapped after raining on a 2D elevation map represented as a matrix. This problem extends the classic "Trapping Rain Water" problem to two dimensions, making the solution more complex due to the need to consider water flow in all directions. Key Points
ApproachThe solution leverages a Breadth-First Search (BFS) approach, guided by a priority queue (min-heap):
Plan
Let's implement this solution in PHP: 407. Trapping Rain Water II <?php
/**
* @param Integer[][] $heightMap
* @return Integer
*/
function trapRainWater($heightMap) {
$m = count($heightMap);
$n = count($heightMap[0]);
if ($m < 3 || $n < 3) {
return 0;
}
$directions = [[1, 0], [-1, 0], [0, 1], [0, -1]];
$minHeap = new SplPriorityQueue();
$visited = array_fill(0, $m, array_fill(0, $n, false));
// Add the boundary cells to the priority queue
for ($i = 0; $i < $m; $i++) {
for ($j = 0; $j < $n; $j++) {
if ($i == 0 || $i == $m - 1 || $j == 0 || $j == $n - 1) {
$minHeap->insert([$i, $j, $heightMap[$i][$j]], -$heightMap[$i][$j]);
$visited[$i][$j] = true;
}
}
}
$waterTrapped = 0;
while (!$minHeap->isEmpty()) {
list($x, $y, $h) = $minHeap->extract();
foreach ($directions as $direction) {
$nx = $x + $direction[0];
$ny = $y + $direction[1];
if ($nx >= 0 && $nx < $m && $ny >= 0 && $ny < $n && !$visited[$nx][$ny]) {
$visited[$nx][$ny] = true;
$waterTrapped += max(0, $h - $heightMap[$nx][$ny]);
$minHeap->insert([$nx, $ny, max($h, $heightMap[$nx][$ny])], -max($h, $heightMap[$nx][$ny]));
}
}
}
return $waterTrapped;
}
// Example Usage
$heightMap1 = [[1, 4, 3, 1, 3, 2], [3, 2, 1, 3, 2, 4], [2, 3, 3, 2, 3, 1]];
$heightMap2 = [[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]];
echo trapRainWater($heightMap1) . "\n"; // Output: 4
echo trapRainWater($heightMap2) . "\n"; // Output: 10
?> Explanation:
Example WalkthroughInput:$heightMap = [
[1, 4, 3, 1, 3, 2],
[3, 2, 1, 3, 2, 4],
[2, 3, 3, 2, 3, 1]
]; Steps:
Time Complexity
Total Complexity:
Output for Example$heightMap = [
[1, 4, 3, 1, 3, 2],
[3, 2, 1, 3, 2, 4],
[2, 3, 3, 2, 3, 1]
];
echo trapRainWater($heightMap); // Output: 4 The "Trapping Rain Water II" problem demonstrates the power of advanced data structures like priority queues combined with BFS. By simulating the flow of water in a 2D elevation map, we can efficiently compute the total trapped water. This solution is optimal for handling large matrices due to its logarithmic heap operations. |
…ons 1513664727 Co-authored-by: kovatz <[email protected]> Co-authored-by: topugit <[email protected]> Co-authored-by: basharul-siddike <[email protected]> Co-authored-by: hafijul233 <[email protected]>
Discussed in #1179
Originally posted by mah-shamim January 19, 2025
Topics:
Array
,Breadth-First Search
,Heap (Priority Queue)
,Matrix
Given an
m x n
integer matrixheightMap
representing the height of each unit cell in a 2D elevation map, return the volume of water it can trap after raining.Example 1:
We have two small ponds 1 and 3 units trapped.
The total volume of water trapped is 4.
Example 2:
Constraints:
m == heightMap.length
n == heightMap[i].length
1 <= m, n <= 200
0 <= heightMap[i][j] <= 2 * 104
The text was updated successfully, but these errors were encountered: