Problem · Array
Trapping Rain Water
Learn this problemProblem statement
Given n non-negative integers representing an elevation map where each bar has width 1, return the total amount of rain water trapped after raining.
Function
trap(height: int[]) → intExamples
Example 1
height = [0,1,0,2,1,0,1,3,2,1,2,1]return = 6The elevation map traps 6 units of water in total.
Example 2
height = [4,2,0,3,2,5]return = 9The bars trap 9 units of water in total.
Constraints
n == height.length1 <= n <= 2 * 10^40 <= height[i] <= 10^5