Trapping Rain Water
Hard
Arrays
Two Pointers
Stack
DP
Given n bars, compute how much water can be trapped.
Input: space-separated heights
Output: total water units
Constraints
See problem-specific constraints. Optimized solution required.
Examples
Example 1:
Input: 0 1 0 2 1 0 1 3 2 1 2 1
Output:
Example 2:
Input: 0 1 0 2 1 0 1 3 2 1 2 1
Output:
Hints
Consider a two pointers approach.
Think about time complexity.
Tests:
Runtime:
Memory:
Test
Input:
Expected:
Got:
Click Run Code to test against sample cases, or Submit to test against all cases.
▲ Console