trappingRainWater

Hard · Array Manipulations · 2 test cases

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

Examples

trappingRainWater([0,1,0,2,1,0,1,3,2,1,2,1]) → 6

Starter code

function trappingRainWater(height) {

}

Complexity of the optimal solution

Time O(n), space O(1). This optimal solution uses a two-pointer approach. The `left` and `right` pointers scan the array from both ends, moving inwards. Since each element is visited exactly once, the time complexity is O(n). It only uses a few variables for pointers and max heights, so space is constant.

Solve trappingRainWater in the browser