Total Rainwater Trapped Across an Elevation Map

Implement totalRainwaterTrapped

You're given an array height where each value is the height of a unit-width bar standing at that position. After it rains, water settles in the dips between bars — compute the total volume of water trapped across the whole map. Try to avoid two extra O(n) arrays for the left/right running maximums — a two-pointer sweep from both ends can get you there in O(1) extra space.

Example 1:

Input: height = [2,0,3,0,4,0,1]

Output: 6

Example 2:

Input: height = [4,1,3,1,5,2]

Output: 7

Example 3:

Input: height = [1,0,2]

Output: 1

+ 5 hidden test cases run on Submit.

Constraints:

  • 1 ≤ height.length ≤ 2 × 10⁴
  • 0 ≤ height[i] ≤ 10⁵

height =

[2, 0, 3, 0, 4, 0, 1]