42. Trapping Rain Water
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:
Example 2:
- n == height.length
- 1 <= n <= 2 * 104
- 0 <= height[i] <= 105
Constraints:
Notes
Intuition
Water at any position is bounded by the minimum of the maximum heights on its left and right. The two-pointer technique lets us compute this without precomputing all prefix/suffix maximums.
Implementation
Use left and right pointers at opposite ends, tracking maxL (max height seen from left) and maxR (max height seen from right). Update these maxes on each iteration. If maxL ≤ maxR, we know the left side is the bottleneck—add maxL - height[l] to the answer and move left. Otherwise, the right side is the bottleneck—add maxR - height[r] and move right.
Edge-cases
The algorithm handles flat terrain (no trapping) naturally since max - height will be zero. Empty arrays or single-element arrays trap no water.
- Time: O(n) — each element visited once
- Space: O(1) — only tracking pointers and two max values