Problem · Array
Trapping Rain Water
Learn this problemProblem statement
You are given an integer array height of length n. n vertical bars stand on the x-axis. The i-th bar has width 1 and height height[i].
Compute how many units of water the bars can trap after rain.
Water sits above a bar only when both a strictly taller left boundary and a strictly taller right boundary exist. The water depth at index i is max(0, min(leftMax[i], rightMax[i]) - height[i]), where leftMax[i] is the tallest bar at an index < i and rightMax[i] is the tallest bar at an index > i.
Function
trap(height: int[]) → intExamples
Example 1
height = [0,1,0,2,1,0,1,3,2,1,2,1]return = 6The bars trap 1 + 1 + 2 + 1 + 1 = 6 units of water.
Example 2
height = [4,2,0,3,2,5]return = 9The valley between the height-4 and height-5 bars traps 2 + 4 + 1 + 2 = 9 units.
Constraints
1 <= height.length <= 2 * 10^4.0 <= height[i] <= 10^5.