Trapping Rain Water
To solve this coding challenge of calculating the amount of trapped rainwater in a given elevation map, we can utilize the concept of finding the maximum heights to the left and right of each element in the array. By comparing these maximum heights, we can determine the water level at each position. Below is a # Explanation section that thoroughly elaborates on the solution along with detailed pseudocode with comments.
and
arrays, and one more pass to calculate the trapped water, we run efficiently even for large arrays. With this detailed explanation and pseudocode, you can clearly understand the logic and easily implement it in any programming language.
Explanation:
- Understanding the Problem : The input is an array where each elementβs value represents the height of bars at each index. Our task is to calculate how much water can be trapped between these bars after it rains. The water trapped at a position depends on the heights of bars to its left and right.
- Calculate Maximum Heights From Left and Right :
- For each position in the array, you need to know the highest bar on its left and the highest bar on its right.
-
Left_max
-
Right_max
- Water Level Calculation :
- For each position, the water level is determined by the minimum of the maximum heights from the left and right.
- If this water level is higher than the height of the bar at that position, the difference (water level minus bar height) is the amount of water that can be stored at that position.
- Summing Up the Water Trapped :
- Sum up the water trapped at each position to get the total amount of trapped water.
- Early Exit :
- If the array is empty, immediately return 0, because no water can be trapped in an empty array.
- Initialize Arrays :
-
Create two arrays,
left_max
right_max
-
Initialize the first element of
left_max
-
Initialize the last element of
right_max
- Fill Left_max Array :
- Iterate through the height array from the second element to the last.
-
For each position, set
left_max[i]
left_max[i-1]
height[i]
- Fill Right_max Array :
- Iterate through the height array from the second last element to the first.
-
For each position, set
right_max[i]
right_max[i+1]
height[i]
- Calculate Total Trapped Water :
-
Initialize
total_water
- Iterate through the height array excluding the first and last elements (since water cannot be trapped at the edges).
-
For each position, calculate the water level as the minimum of
left_max[i]
right_max[i]
-
If this water level is greater than the height at that position, add the difference to
total_water
- Return Result :
-
Return the
total_water
# Check if the height array is empty
if height array is empty:
return 0
# Initialize left_max and right_max arrays
set left_max to an array of zeros of the same length as the height array
set right_max to an array of zeros of the same length as the height array
# Initialize the first element of left_max and the last element of right_max
left_max[0] = height[0]
right_max[-1] = height[-1]
# Fill the left_max array
for each element in height array starting from the second element:
left_max[i] = maximum of left_max[i-1] and height[i]
# Fill the right_max array
for each element in height array starting from the second last element to the first:
right_max[i] = maximum of right_max[i+1] and height[i]
# Initialize total trapped water to 0
total_water = 0
# Calculate water trapped at each position
for each element in height array excluding the first and last elements:
water_level = minimum of left_max[i] and right_max[i]
if water_level > height[i]:
total_water += water_level - height[i]
# Return the total trapped water
return total_water
This algorithm ensures that by making two passes through the height array to fill the
left_max
right_max