Trapping Rain Water

Difficulty: Hard

You're given an elevation map as a list of bar heights. Each bar has width 1 and sits right next to the next. After it rains, how many units of water are trapped between the bars?

Water can only sit above a bar if something tall enough is on both its left and right to hold it in. Otherwise it runs off.

Examples

Input: height = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]
Output: 6

Water pools above the low spots wherever there are taller bars on both sides; adding up the water trapped at every position gives 6 total units.

Input: height = [4, 2, 0, 3, 2, 5]
Output: 9

Input: height = [1, 2, 3]
Output: 0

The elevation only ever rises, so there's never a wall on both sides of a low spot — nothing gets trapped.

Constraints

  • 1 <= height.length <= 2 * 10^5

  • 0 <= height[i] <= 10^5

Approach

The water above a bar depends on the shorter of the tallest bar to its left and the tallest bar to its right, since water spills out over the shorter side first. The direct approach rescans outward from every position to find these, which repeats a lot of work.

A better approach precomputes the tallest bar so far from the left and from the right in two passes, stores them in two arrays, and combines them in a third pass. It's fast but uses O(n) extra memory.

The optimal approach skips the arrays. Walk two pointers inward from both ends, keeping a running "tallest so far" for each side. The smaller of the two running maximums is always the true limiting wall for the pointer on that side. So you can compute that position's water right away and move that pointer inward.

Solutions

Brute Force — Scan Left and Right From Every Bar

For each position, scan left and right to find the tallest bar on each side. The shorter of the two sets how much water sits there.

function trap(height) {
  let total = 0;
  const n = height.length;

  for (let i = 0; i < n; i++) {
    let leftMax = 0;
    for (let l = 0; l <= i; l++) leftMax = Math.max(leftMax, height[l]);

    let rightMax = 0;
    for (let r = i; r < n; r++) rightMax = Math.max(rightMax, height[r]);

    total += Math.min(leftMax, rightMax) - height[i];
  }

  return total;
}

Time: O(n²) · Space: O(1)

Better — Precomputed Left/Right Max Arrays

One left-to-right pass records the tallest bar up to each position. One right-to-left pass records the tallest bar from each position onward. A final pass combines them.

function trap(height) {
  const n = height.length;
  if (n === 0) return 0;

  const leftMax = new Array(n);
  leftMax[0] = height[0];
  for (let i = 1; i < n; i++) {
    leftMax[i] = Math.max(leftMax[i - 1], height[i]);
  }

  const rightMax = new Array(n);
  rightMax[n - 1] = height[n - 1];
  for (let i = n - 2; i >= 0; i--) {
    rightMax[i] = Math.max(rightMax[i + 1], height[i]);
  }

  let total = 0;
  for (let i = 0; i < n; i++) {
    total += Math.min(leftMax[i], rightMax[i]) - height[i];
  }

  return total;
}

Time: O(n) · Space: O(n)

Optimal — Two Pointers

Walk two pointers inward from both ends, tracking the maximum height seen on each side. Resolve the side with the smaller running maximum next: that maximum is guaranteed to be the true limiting wall for its position.

function trap(height) {
  let left = 0;
  let right = height.length - 1;
  let leftMax = 0;
  let rightMax = 0;
  let total = 0;

  while (left < right) {
    if (height[left] < height[right]) {
      leftMax = Math.max(leftMax, height[left]);
      total += leftMax - height[left];
      left++;
    } else {
      rightMax = Math.max(rightMax, height[right]);
      total += rightMax - height[right];
      right--;
    }
  }

  return total;
}

Time: O(n) · Space: O(1)