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.length1 <= n <= 2 * 1040 <= height[i] <= 105When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine looking at a street of buildings of different heights after it has rained, and you want to figure out how much water gets trapped in the dips between them. The brute force approach looks at every single spot individually to see how much water can sit directly above it.
Here's how the algorithm would work step-by-step:
def trap(height_list):
total_water = 0
# Iterate through every building to calculate trapped water individually at each specific position
for current_index in range(len(height_list)):
max_left_height = 0
max_right_height = 0
for left_scanner in range(current_index, -1, -1):
if height_list[left_scanner] > max_left_height:
max_left_height = height_list[left_scanner]
for right_scanner in range(current_index, len(height_list)):
if height_list[right_scanner] > max_right_height:
max_right_height = height_list[right_scanner]
# Water overflows past the shorter bounding wall, so we must find the limiting boundary height
limiting_boundary = min(max_left_height, max_right_height)
if limiting_boundary > height_list[current_index]:
# Accumulate water volume by subtracting the current building height from the bounding height
total_water += limiting_boundary - height_list[current_index]
return total_waterInstead of checking every puddle one by one, the optimal strategy uses two boundary markers moving from the outer edges toward the center. By always moving the shorter boundary inward, we can instantly know how much water a spot can hold without needing to look all the way across the entire landscape.
Here's how the algorithm would work step-by-step:
def trap(height_landscape):
if not height_landscape:
return 0
left_marker = 0
right_marker = len(height_landscape) - 1
tallest_left = 0
tallest_right = 0
accumulated_water = 0
# The pointer with the smaller height limits the trapped water level.
while left_marker < right_marker:
if height_landscape[left_marker] < height_landscape[right_marker]:
if height_landscape[left_marker] >= tallest_left:
tallest_left = height_landscape[left_marker]
else:
accumulated_water += tallest_left - height_landscape[left_marker]
left_marker += 1
else:
if height_landscape[right_marker] >= tallest_right:
tallest_right = height_landscape[right_marker]
else:
accumulated_water += tallest_right - height_landscape[right_marker]
right_marker -= 1
return accumulated_water| Case | How to Handle |
|---|---|
| Empty array or null input | Return zero immediately as no water can be trapped without any elevation bars. |
| Arrays with fewer than 3 elements | Return zero because at least three bars are required to form a depression that holds water. |
| Monotonically increasing or decreasing arrays | Return zero since water will simply spill off the edges without any enclosed basin. |
| Arrays with all identical non-zero heights | Return zero because flat terrain lacks the varying heights needed to form traps. |
| Massive inputs with millions of elements | Use an O(n) time and O(1) space two-pointer approach to avoid memory and time limits. |
| Extremely tall elevation values | Use 64-bit integer types to prevent arithmetic overflow when accumulating large water volumes. |
| Inputs containing zero-height bars | Process them normally as valley floors that contribute directly to the trapped water volume. |
| Arrays with a single deep basin in the center | The two-pointer technique naturally converges on the maximum height and accumulates the correct trapped volume. |