Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

Trapping Rain Water

Hard
two-pointersarraystackdynamic-programming

Given n non-negative integers representing an elevation map where each bar has width 1, compute how much water it can trap after raining.

Input: one line of space-separated integers height. Output: total units of trapped water.

Example 1
Input:
0 1 0 2 1 0 1 3 2 1 2 1
Output:
6
Example 2
Input:
4 2 0 3 2 5
Output:
9

Constraints

  • 1 ≤ height.length ≤ 2·10^4
  • 0 ≤ height[i] ≤ 10^5
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.