Problem
Section titled “Problem”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:
![Elevation map [0,1,0,2,1,0,1,3,2,1,2,1] with 6 units of trapped water](https://assets.leetcode.com/uploads/2018/10/22/rainwatertrap.png)
Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]Output: 6Explanation: 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: 9Constraints:
n == height.length-
height[i]
Solution approach
Section titled “Solution approach”Intuition
Section titled “Intuition”Look at a single column i. Water above it rises until it spills over the lower of the two walls around it, so
where maxLeft[i] is the tallest bar in height[0..i] and maxRight[i] the tallest in height[i..n-1]. Precomputing both arrays gives an -space solution. We can drop the arrays with two pointers left and right moving towards each other, keeping leftMax (tallest bar seen from the left so far) and rightMax (tallest seen from the right).
The key observation: suppose height[left] < height[right]. Every bar that left has passed was lower than some bar still on the right (that is why left moved), so leftMax is at most maxRight[left]; and height[right] itself shows the right side is taller than the current bar. Hence min(maxLeft, maxRight) at left is exactly leftMax, and the water above left is leftMax - height[left] — no need to know the full right side. The mirror argument holds for right when height[left] >= height[right].
So at each step we settle the column on the lower side, whose water level is already determined, and move that pointer inward.
Algorithm
Section titled “Algorithm”- Set
left = 0,right = n - 1, andleftMax = rightMax = water = 0. - While
left < right:- If
height[left] < height[right]: updateleftMax = max(leftMax, height[left]), addleftMax - height[left]towater, and incrementleft. - Otherwise: update
rightMax = max(rightMax, height[right]), addrightMax - height[right]towater, and decrementright.
- If
- Return
water. The column where the pointers meet is the tallest bar and holds no water.
Walkthrough
Section titled “Walkthrough”Purple cells are bars, blue cells are water already counted, and green heights are settled columns:
Complexity
Section titled “Complexity”- Time: — each iteration moves one pointer one step inward, so there are
n - 1iterations. - Space: — four integers.
Edge cases
Section titled “Edge cases”- Fewer than three bars (
n = 1orn = 2): no column has walls on both sides; the loop runs at most once and adds0because the running maximum equals the current bar. - Monotonic heights such as
[1,2,3]: each settled column is the new maximum on its side, so it adds0. - Overflow: the total is at most , so
intis enough.
Think before you peek
Work out an approach on your own first — the explanation will still be here when you want it.
class Solution { public: int trap(vector<int>& height) { int left = 0, right = (int)height.size() - 1; int leftMax = 0, rightMax = 0, water = 0; while (left < right) { if (height[left] < height[right]) { leftMax = max(leftMax, height[left]); water += leftMax - height[left]; left++; } else { rightMax = max(rightMax, height[right]); water += rightMax - height[right]; right--; } } return water; }};class Solution { public int trap(int[] height) { int left = 0, right = height.length - 1; int leftMax = 0, rightMax = 0, water = 0; while (left < right) { if (height[left] < height[right]) { leftMax = Math.max(leftMax, height[left]); water += leftMax - height[left]; left++; } else { rightMax = Math.max(rightMax, height[right]); water += rightMax - height[right]; right--; } } return water; }}class Solution: def trap(self, height: list[int]) -> int: left, right = 0, len(height) - 1 left_max = right_max = water = 0 while left < right: if height[left] < height[right]: left_max = max(left_max, height[left]) water += left_max - height[left] left += 1 else: right_max = max(right_max, height[right]) water += right_max - height[right] right -= 1 return waterGive it a try first
Attempt the problem on LeetCode — the code will still be here.
![Two pointers settling columns of [0,1,0,2,1,0,1,3,2,1,2,1] from the lower side, step 1 of 13](/CodePrime/img/codes/trapping-rain-water/two-pointers/01.webp)
![Two pointers settling columns of [0,1,0,2,1,0,1,3,2,1,2,1] from the lower side, step 2 of 13](/CodePrime/img/codes/trapping-rain-water/two-pointers/02.webp)
![Two pointers settling columns of [0,1,0,2,1,0,1,3,2,1,2,1] from the lower side, step 3 of 13](/CodePrime/img/codes/trapping-rain-water/two-pointers/03.webp)
![Two pointers settling columns of [0,1,0,2,1,0,1,3,2,1,2,1] from the lower side, step 4 of 13](/CodePrime/img/codes/trapping-rain-water/two-pointers/04.webp)
![Two pointers settling columns of [0,1,0,2,1,0,1,3,2,1,2,1] from the lower side, step 5 of 13](/CodePrime/img/codes/trapping-rain-water/two-pointers/05.webp)
![Two pointers settling columns of [0,1,0,2,1,0,1,3,2,1,2,1] from the lower side, step 6 of 13](/CodePrime/img/codes/trapping-rain-water/two-pointers/06.webp)
![Two pointers settling columns of [0,1,0,2,1,0,1,3,2,1,2,1] from the lower side, step 7 of 13](/CodePrime/img/codes/trapping-rain-water/two-pointers/07.webp)
![Two pointers settling columns of [0,1,0,2,1,0,1,3,2,1,2,1] from the lower side, step 8 of 13](/CodePrime/img/codes/trapping-rain-water/two-pointers/08.webp)
![Two pointers settling columns of [0,1,0,2,1,0,1,3,2,1,2,1] from the lower side, step 9 of 13](/CodePrime/img/codes/trapping-rain-water/two-pointers/09.webp)
![Two pointers settling columns of [0,1,0,2,1,0,1,3,2,1,2,1] from the lower side, step 10 of 13](/CodePrime/img/codes/trapping-rain-water/two-pointers/10.webp)
![Two pointers settling columns of [0,1,0,2,1,0,1,3,2,1,2,1] from the lower side, step 11 of 13](/CodePrime/img/codes/trapping-rain-water/two-pointers/11.webp)
![Two pointers settling columns of [0,1,0,2,1,0,1,3,2,1,2,1] from the lower side, step 12 of 13](/CodePrime/img/codes/trapping-rain-water/two-pointers/12.webp)
![Two pointers settling columns of [0,1,0,2,1,0,1,3,2,1,2,1] from the lower side, step 13 of 13](/CodePrime/img/codes/trapping-rain-water/two-pointers/13.webp)