Trapping Rain Water
Problem statement
Nonnegative bar heights describe a one-dimensional elevation map with unit-width bars. Return the total units of rainwater retained between the bars after rain stops. Water can escape from either end; arrays with fewer than three bars retain no water.
Function
trappedWater(height: int[]) → intExamples
Example 1
height = [0,1,0,2,1,0,1,3,2,1,2,1]return = 6The retained depths at indices 2, 4, 5, 6 and 9 are 1, 1, 2, 1 and 1, totaling 6.
Example 2
height = [3,0,2,0,4]return = 7Interior depths are 3, 1 and 3, totaling 7.
Constraints
0 <= height.length <= 1000.0 <= height[i] <= 1000.