Imagine you're an architect designing a system to collect rainwater in a cityscape with varying building heights. You have an elevation map represented by an array of non-negative integers, where each integer represents the height of a building and each building has a uniform width of 1 unit.

After it rains, water gets trapped between the buildings, forming pools. Your task is to calculate the total amount of rainwater that can be trapped in this urban landscape.

Goal: Given an array of heights, return the total units of water that can be trapped.

Example: For heights [0,1,0,2,1,0,1,3,2,1,2,1], water gets trapped between buildings, and the total trapped water is 6 units.

Input & Output

example_1.py โ€” Standard Case
$ Input: [0,1,0,2,1,0,1,3,2,1,2,1]
โ€บ Output: 6
๐Ÿ’ก Note: The elevation map traps water in valleys: 1 unit at index 2, 1 unit at index 5, 2 units at index 6, 1 unit at index 8, and 1 unit at index 9. Total = 6 units.
example_2.py โ€” Simple Valley
$ Input: [3,0,2,0,4]
โ€บ Output: 7
๐Ÿ’ก Note: Water gets trapped at indices 1 and 3. At index 1: min(3,4) - 0 = 3 units. At index 3: min(2,4) - 0 = 2 units. Total = 5 units. Wait, let me recalculate: At index 1: min(3,4) - 0 = 3. At index 2: min(3,4) - 2 = 1. At index 3: min(3,4) - 0 = 3. Total = 7 units.
example_3.py โ€” No Water Trapped
$ Input: [1,2,3,4,5]
โ€บ Output: 0
๐Ÿ’ก Note: This is a monotonically increasing sequence, so no water can be trapped. Water would flow off to the left side.

Visualization

Tap to expand
Trapping Rain WaterWater (blue) collects in valleys between buildings (brown)1 unit1 unit๐Ÿ’ง Rain falls everywhere, but only gets trapped between higher barriers
Understanding the Visualization
1
Identify the Landscape
We have buildings of different heights creating valleys and peaks
2
Rain Falls
Water falls uniformly across the entire landscape
3
Water Settles
Water gets trapped in valleys between buildings, filling up to the level where it would overflow
4
Calculate Volume
For each position, water level = min(max_left_height, max_right_height)
Key Takeaway
๐ŸŽฏ Key Insight: Water level at any position is limited by the shorter of the tallest barriers on its left and right sides. The two-pointer technique elegantly exploits this by always moving from the side with the smaller maximum height.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Three separate O(n) passes through the array

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Two additional arrays to store left and right maximum heights

n
2n
โšก Linearithmic Space

Constraints

  • n == height.length
  • 1 โ‰ค n โ‰ค 2 ร— 104
  • 0 โ‰ค height[i] โ‰ค 3 ร— 104
  • Follow-up: Can you solve it in O(1) extra space?
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 25 Apple 18
42.4K Views
Very High Frequency
~18 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen