Skip to content
Onsite

Trapping Rain Water

HardP1Coding

height[i] is the height of an elevation bar of width 1. After rain, water collects between the bars. Return the total units of water trapped.

Example 1

Input
height = [0,1,0,2,1,0,1,3,2,1,2,1]
Output
6

Example 2

Input
height = [4,2,0,3,2,5]
Output
9

Example 3

Input
height = [3,0,0,2,0,4]
Output
10

Constraints

  • 1 <= height.length <= 2 * 10^4
  • 0 <= height[i] <= 10^5

Hints

Run checks the 3 visible tests, Submit all 8.