Skip to content

Practice Trapping Rain Water

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#42
LeetCode Problem

Trapping Rain Water

Problem Statement

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.

Source: Trapping Rain Water on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

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

n == height.length
1 <= n <= 2 * 10^4
0 <= height[i] <= 10^5

How to solve Trapping Rain Water

Original notes for interview preparation, written for this problem. Reviewed .

Key insight

The water above a bar is the smaller of the tallest bar to its left and the tallest to its right, minus its own height; with pointers moving inward from both ends, the side with the smaller current height can be settled at once because the other side holds a bar at least that tall.

Why brute force is not enough

For every bar scan left and right for the maximum heights, then add the smaller maximum minus the bar's height. That is O(n^2), about 4 x 10^8 operations for n up to 2 x 10^4, too slow in many languages.

Approach

  1. Put a left pointer at index 0 and a right pointer at index n - 1, and track leftMax and rightMax, the tallest bars seen from each end, both starting at zero.
  2. While left is before right, compare the two current heights. If the left height is at most the right height, the left bar's level is set by leftMax alone (the right side holds a bar at least as tall): raise leftMax if needed, add leftMax minus the left height, and advance left.
  3. Otherwise mirror on the right: raise rightMax if needed, add rightMax minus the right height, and retreat right.
  4. The invariant is that every bar already passed has had its exact water counted. Return the total when the pointers meet.

Walkthrough of Example 2

height = [4,2,0,3,2,5] → 9

  1. Left 0 (4) versus right 5 (5): 4 <= 5, so leftMax becomes 4, water 0, and left moves to 1.
  2. Left 1 (2), then 2 (0), then 3 (3): each is at most the right height 5, leftMax stays 4, and the additions are 2, 4, and 1 for a running total of 7.
  3. Left 4 (2): add 4 - 2 = 2 (total 9). Left reaches 5 and meets right; return 9.

Time and space

Time O(n) · Space O(1). Each iteration advances one pointer, so at most n - 1 iterations of constant work, where n is the number of bars; only two indices and two maxima are stored.

Interview pitfalls

  • Comparing leftMax with rightMax in one place and the current heights in another to choose which side to move; each works alone, but mixing them gives wrong totals.
  • Computing water before updating the running maximum, giving a negative contribution when the current bar is the new tallest.
  • Expecting a single bar or a monotonic slope to trap water; both must return 0.
  • Accumulating in a 32-bit integer: with n near 2 x 10^4 and heights up to 10^5 the total nears 2 x 10^9, so use 64-bit in C, C++, or Java.

Follow-up questions interviewers ask

  • How would you solve it with a monotonic stack, which counts water by horizontal layers instead of columns?
  • How does the 2D version (Trapping Rain Water II) replace the two pointers with a heap over the boundary?
  • If heights arrive as a stream with no view of the right side, what can and cannot be computed?

Related LeetCode Problems

Practice more problems similar to Trapping Rain Water.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.