42 · two pointers · hard
Trapping rain water
Each number is a wall of width 1. It rains. Water pools in the dips, anything that can run off either end drains away. How many units are left?
The one rule
Stop thinking about pools. Think about one column at a time. Water above a column rises until it spills over the lower of two walls: the tallest bar anywhere to its left, and the tallest bar anywhere to its right. Not the neighbors — the tallest thing on each side, because water can sit on top of shorter bars on the way there.
water[i] = min(maxLeft, maxRight) - height[i] // clamped at 0
Everything else falls out of that line. The two end columns have no wall on one side, so they hold nothing. A single wall like [0, 0, 1, 0, 0] holds nothing — water needs a taller bar on both sides. A slope like [1, 2, 3] holds nothing. And a bar taller than the water line is what the clamp is for.
Two arrays · O(n) time, O(n) space
Brute force rescans both sides for every column — O(n²). The fix is noticing that the tallest wall to the left of i is either the tallest wall to the left of i − 1, or bar i − 1 itself. So one left-to-right sweep fills maxLeft, one right-to-left sweep fills maxRight, and a third loop takes the min and adds up the water. Each wall gets looked at once.
Step through it on [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]. The line under the picture is the exact line of code running, with real numbers plugged in.
var trap = function (height) {
let n = height.length;
let maxLeft = new Array(n).fill(0);
let maxRight = new Array(n).fill(0);
for (let i = 1; i < n; i++) {
maxLeft[i] = Math.max(maxLeft[i - 1], height[i - 1]);
}
for (let i = n - 2; i >= 0; i--) {
maxRight[i] = Math.max(maxRight[i + 1], height[i + 1]);
}
let res = 0;
for (let i = 0; i < n; i++) {
res += Math.max(0, Math.min(maxLeft[i], maxRight[i]) - height[i]);
}
return res;
};
Two details that are easy to get backwards. The maxLeft loop starts at i = 1 and reads height[i − 1], not height[i]: "tallest on the left" means strictly left, so index 0 stays at its fill(0) default and a column never counts itself. And loops 1 and 2 never touch water — they only answer "what's the tallest thing on each side of me?" for every column, once. Loop 3 is the one rule, with the lookups already paid for.
Two pointers · O(n) time, O(1) space
Two arrays spend O(n) space to look up two numbers per column. But min only cares about the shorter wall. If leftMax < rightMax, then whatever the true tallest-right for column l turns out to be, it's at least rightMax — which is already bigger. So column l's water line is exactly leftMax, and it can be settled without ever finding its real right wall. Settle the shorter side, step it inward, repeat until the pointers meet.
Same input. Watch which side moves, and notice the far wall is never looked up — only "something at least this tall exists over there."
function trap(height) {
let l = 0, r = height.length - 1;
let leftMax = height[l], rightMax = height[r];
let water = 0;
while (l < r) {
if (leftMax < rightMax) {
l++;
leftMax = Math.max(leftMax, height[l]);
water += leftMax - height[l];
} else {
r--;
rightMax = Math.max(rightMax, height[r]);
water += rightMax - height[r];
}
}
return water;
}
The clamp disappears because leftMax is refreshed with height[l] before subtracting, so the difference is never negative. The two end columns are never processed at all — they seed leftMax and rightMax, and they can't hold water anyway.
Complexity
| approach | time | space |
|---|---|---|
| brute force · rescan both sides per column | O(n²) | O(1) |
| two arrays | O(n) | O(n) |
| two pointers | O(n) | O(1) |
Edge cases
[0, 0, 1, 0, 0]→ 0. One wall is not a container.[1, 2, 3]or[3, 2, 1]→ 0. Monotonic slopes drain off one end.[3, 3, 3]→ 0. Plateaus have nothing to fill.[3, 0, 3]→ 3. The smallest real container.n = 1→ 0. Both loops never run.