CodeMentor.
Ask AIProblemsLearnC++TCSCore CSHRProgress
0Lv 1
ProblemsTwo 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++17Autosaves