17. Trapping Rain Water
Each height value describes a bar of width 1. Compute the total amount of rainwater held between the bars after rainfall.
Count water above every position; bars keep their original order.
Example 1
Input
[4,2,0,3,2,5]
Output
9
Explanation: Per-index volumes are [0,2,4,1,2,0].
Example 2
Input
[3,0,3]
Output
3
Explanation: The middle unit-width cell holds three units.
Constraints
1 <= height.length <= 2 * 1040 <= height[i] <= 105- Each bar has width 1. Return the total trapped water.
Hints
Hint 1
Write the per-index formula using left and right maxima.
Hint 2
If an established left wall is lower than an established right wall, which side can be finalized?
Hint 3
Process the smaller established maximum, update it, and add its difference from the chosen bar.
Submissions
Your recent submissions for this problem.
Checking sign-in…
Discussion
Ask a question or share an approach. Posts are public; avoid personal information and mark solution spoilers clearly.
No discussion yet. Start the conversation with a question about this problem.
Checking sign-in…
Run your code to check the sample cases, or submit it to check the full validation suite.
Finalize cells against established walls
[4,2,0,3,2,5]
9