2Brute2Best
🔥0

Trapping Rain Water

Hard·LeetCode #42 Pro OnlyTricky / Edge Case
LeetCode
01

Problem Statement

You are given n bars standing side by side, each 1 unit wide. height[i] is the height of bar i.

Rain falls on them. Water collects in the dips between taller bars, and anything that isn't held in by a bar on both sides flows away.

Return the total amount of water trapped.

Input

An integer array height of length n.

Output

One integer: the total units of trapped water.

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

Water per cell is [0, 0, 1, 0, 1, 2, 1, 0, 0, 1, 0, 0], which adds up to 6.

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

The walls 4 and 5 hold the water. Per cell: [0, 2, 4, 1, 2, 0] = 9.

Constraints
  • • 1 ≤ n ≤ 2 · 10^4
  • • 0 ≤ height[i] ≤ 10^5
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“water trapped between bars”
→ Each cell's water = min(tallest on left, tallest on right) − its height → a per-cell formula that needs info from both sides.
“tallest on the left / right”
→ Running maxes from both ends. Two pointers walking inward can carry one running max each.
“limited by the lower wall”
→ The side with the smaller max is the one whose cells can be decided now → move that pointer.
03

Approach Complexity Ladder

Brute Force — scan both sides for every cellO(N²)O(1)

For each cell, scan left for the tallest bar and right for the tallest bar

Better — prefix max and suffix max arraysO(N)O(N)

Precompute the tallest bar up to each index from both sides

Optimal — two pointers with leftMax and rightMaxO(N)O(1)

Settle the cell on the side whose running max is smaller

Brute Force Intuition

For each cell i, scan everything to its left to find the tallest bar, and everything to its right to find the tallest bar (both including i). The water stands at the lower of those two walls, so cell i holds min(leftMax, rightMax) − height[i]. Add it up.

Recognition rule for this sub-pattern:

Start at both ends of a sorted (or symmetric) structure. Each step, move the side that cannot do better.
Remember this · Two Pointers
Trigger
Sorted array, pairs/triplets with a target, comparing from both ends, compacting an array in place.
Think
If I compare the elements at two positions, can I safely discard one side?
Key question
Which pointer moves, and why can the skipped elements never be the answer?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Two Pointers translate into production architectures and low-level memory mechanics.