Trapping Rain Water
Hard
Topics
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
Each bar is one elevation value. The shaded blocks are the water that is trapped, which is what the answer counts — a column holds water up to
min(tallest to its left, tallest to its right).Input: height = [0,1,0,2,1,0,1,3,2,1,2,1] Output: 6
Example 2
Input: height = [4,2,0,3,2,5] Output: 9
Constraints
- 1 <= n <= 2*10^4
- 0 <= height[i] <= 10^5
Editorial — intuition, approach, complexity and pitfalls
Intuition
Water sitting above a bar equals min(tallest wall on its left, tallest wall on its right) - the bar’s own height.
With two pointers we always process the side whose max is definitely the limiting one: if the left bar is shorter than the right bar, the left side’s water depends only on leftMax, so we can settle it immediately.
Approach
- Pointers
l, rplusleftMax,rightMax. - If
height[l] < height[r]: updateleftMax, addleftMax - height[l], movelright. - Else: update
rightMax, addrightMax - height[r], moverleft. - Accumulate until the pointers meet.
Walkthrough
height = [4, 2, 0, 3, 2, 5]
right side lower until the 5 at the end;
trapped: above the 2 -> 2, above 0 -> 3, above 3 -> 0, above 2 -> 1... total = 9Complexity
- Time — O(n): a single two-pointer pass.
- Space — O(1): a few scalars (the precompute-arrays version costs O(n)).
Common Pitfalls
- Update the running max before adding water, or you may subtract more than the wall height.
Full Solution — all languages
class Solution:
def trap(self, nums):
if not nums:
return 0
lo, hi = 0, len(nums) - 1
lmax, rmax, res = nums[lo], nums[hi], 0
while lo < hi:
if lmax < rmax:
lo += 1
lmax = max(lmax, nums[lo])
res += lmax - nums[lo]
else:
hi -= 1
rmax = max(rmax, nums[hi])
res += rmax - nums[hi]
return resimport java.util.*;
class Solution {
public int trap(int[] nums) {
if (nums.length == 0) return 0;
int lo = 0, hi = nums.length - 1, lmax = nums[0], rmax = nums[hi], res = 0;
while (lo < hi) {
if (lmax < rmax) {
lo++;
lmax = Math.max(lmax, nums[lo]);
res += lmax - nums[lo];
} else {
hi--;
rmax = Math.max(rmax, nums[hi]);
res += rmax - nums[hi];
}
}
return res;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int trap(vector<int>& nums) {
if (nums.empty()) return 0;
int lo = 0, hi = (int)nums.size() - 1, lmax = nums[0], rmax = nums[hi], res = 0;
while (lo < hi) {
if (lmax < rmax) {
lo++;
lmax = max(lmax, nums[lo]);
res += lmax - nums[lo];
} else {
hi--;
rmax = max(rmax, nums[hi]);
res += rmax - nums[hi];
}
}
return res;
}
};/**
* @param {number[]} nums
* @return {number}
*/ var trap = function(nums) {
if (nums.length === 0) return 0;
let lo = 0, hi = nums.length - 1, lmax = nums[0], rmax = nums[hi], res = 0;
while (lo < hi) {
if (lmax < rmax) {
lo++;
lmax = Math.max(lmax, nums[lo]);
res += lmax - nums[lo];
} else {
hi--;
rmax = Math.max(rmax, nums[hi]);
res += rmax - nums[hi];
}
}
return res;
};