Trapping Rain Water

Read the lesson → understand the pattern → then code. Tutor works offline (built-in teacher); optional Cursor/Gemini keys upgrade chat quality.

Topic refresher: Two Pointers — Two Indices, One Rule

Move left/right based on a condition instead of checking all pairs.

int L = 0, R = n - 1;
while (L < R) {
    int sum = a[L] + a[R];
    if (sum == target) { L++; R--; }
    else if (sum < target) L++;
    else R--;
}
Two Pointers

Trapping Rain Water

Hard
LeetCode
7/7
HardTwo PointersLeetCode #42

Trapping Rain Water

Water at i = max(0, min(leftMax,rightMax)-height[i]). Two pointers / pref-max variant.

Examples

Example 1

Input

12
0 1 0 2 1 0 1 3 2 1 2 1

Output

6
Example 2

Input

6
4 2 0 3 2 5

Output

9

Stuck? Take a nudge

C++17