0:00 / 0:00

How much rain pools between the bars? Walk in from both ends, always moving the side with the lower wall, whose water is already decided.

▼

The problem

LeetCode 42 (Hard). Given the heights of a row of bars, each one unit wide, how much rain water is trapped between them? The water above bar i is min(tallest bar on its left, tallest bar on its right) - height[i] (counting the bar itself on both sides).

Example: [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1] → 6.

def trap(height):
    left, right = 0, len(height) - 1
    left_max = right_max = water = 0
    while left < right:
        left_max = max(left_max, height[left])
        right_max = max(right_max, height[right])
        if left_max < right_max:
            water += left_max - height[left]
            left += 1
        else:
            water += right_max - height[right]
            right -= 1
    return water
TRY IT ON LEETCODE ▶

The solution

def trap(height):
    left, right = 0, len(height) - 1
    left_max = right_max = water = 0
    while left < right:
        left_max = max(left_max, height[left])
        right_max = max(right_max, height[right])
        if left_max < right_max:
            water += left_max - height[left]
            left += 1
        else:
            water += right_max - height[right]
            right -= 1
    return water

Transcript

Trapping Rain Water. You get a row of bars, each one unit wide. When it rains, water pools in the dips between them. How much water stays trapped?

Take zero, one, zero, two, one, zero, one, three, two, one, two, one. Let it rain: water fills the dips and spills off both ends. Six squares stay trapped.

Look at one bar. Its water rises only as high as the lower of two walls: the tallest bar on its left, and the tallest on its right. Here, two and three, so the water stops at two. Minus the bar's height, zero: two squares.

The simple way scans left and right for every bar. That's up to n steps per bar, so n squared in all.

Better: put a pointer at each end, and let each remember the tallest bar it has seen. Say the left max is two and the right max is three. The right side has a wall at least three tall, so the left bar's water depends only on the left max. It's already decided. So always move the side with the smaller max, and add its water.

In code: while the pointers haven't met, update both maxes. If the left max is smaller, add the left max minus the left bar, and step inward. Otherwise, do the same on the right. Return the total.

Let's run it. Left max zero, right max one, so the left moves. One and one is a tie, and ties go right. Now the right max is two, so the left moves twice: zero, then one square. Both maxes are two, so the right moves: zero, one, zero. Then the right pointer reaches the three, and the left does the rest: zero, one, two, one. The pointers meet. Six squares.

Every step moves one pointer inward, so each bar is visited once: linear time. Two pointers, two maxes and a total: constant space.

Two pointers, two walls, and the lower one decides. That's Trapping Rain Water.