Given n
non-negative integers representing an elevation map where the width of each bar is 1
, compute how much water it can trap after raining.
Example 1:
Input: height = [0,1,0,2,1,0,1,3,2,1,2,1] Output: 6 Explanation: The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.
Example 2:
Input: height = [4,2,0,3,2,5] Output: 9
Constraints:
n == height.length
1 <= n <= 2 * 104
0 <= height[i] <= 105
The core challenge of this problem is to determine how much water can be trapped between the bars of the elevation map after it rains. This problem is significant in various fields such as civil engineering for designing drainage systems and computer graphics for simulating realistic environments.
Potential pitfalls include misunderstanding how water is trapped between bars and not accounting for all possible trapped water units.
To solve this problem, we need to consider the height of the bars and how they form valleys where water can be trapped. Here are the steps to approach the solution:
The naive solution involves iterating through each bar and calculating the trapped water by finding the maximum height to the left and right of each bar. This approach is not optimal because it requires O(n^2) time complexity due to the nested loops.
We can optimize the solution using two pointers and precomputed arrays for the maximum heights to the left and right of each bar. This reduces the time complexity to O(n).
In this approach, we use two pointers, one starting from the left and the other from the right. We also maintain two variables to store the maximum heights encountered so far from both directions. By comparing the heights at the pointers, we can determine the trapped water at each step.
Here is a step-by-step breakdown of the two-pointer algorithm:
def trap(height):
# Initialize pointers and variables to store max heights
left, right = 0, len(height) - 1
left_max, right_max = 0, 0
water_trapped = 0
# Loop until the two pointers meet
while left < right:
if height[left] < height[right]:
if height[left] >= left_max:
left_max = height[left] # Update left_max
else:
water_trapped += left_max - height[left] # Calculate trapped water
left += 1 # Move left pointer to the right
else:
if height[right] >= right_max:
right_max = height[right] # Update right_max
else:
water_trapped += right_max - height[right] # Calculate trapped water
right -= 1 # Move right pointer to the left
return water_trapped
# Example usage
height1 = [0,1,0,2,1,0,1,3,2,1,2,1]
height2 = [4,2,0,3,2,5]
print(trap(height1)) # Output: 6
print(trap(height2)) # Output: 9
The time complexity of the two-pointer approach is O(n) because we traverse the array only once. The space complexity is O(1) as we use a constant amount of extra space.
Potential edge cases include:
Examples:
height = [1]
Output: 0
height = [2, 2, 2, 2]
Output: 0
To test the solution comprehensively, include a variety of test cases:
Example test cases:
assert trap([0,1,0,2,1,0,1,3,2,1,2,1]) == 6
assert trap([4,2,0,3,2,5]) == 9
assert trap([1]) == 0
assert trap([2, 2, 2, 2]) == 0
When approaching such problems, consider the following tips:
In this blog post, we discussed the problem of trapping rainwater, explored different approaches to solve it, and provided a detailed explanation of the optimized two-pointer solution. Understanding and solving such problems is crucial for developing strong problem-solving skills in programming.
We encourage readers to practice and explore further to enhance their understanding and proficiency.
For further reading and practice, consider the following resources: