trapping-rain-water.sh — zsh
arraystwo-pointersstackdynamic-programming

Each non-negative value in height is a unit-width elevation bar. Return the total water units retained after rain.

Input / output

  • Input: height: integer[]
  • Output: integer water volume

Examples

  1. [0,1,0,2,1,0,1,3,2,1,2,1] returns 6.
  2. [4,2,0,3,2,5] returns 9.

Constraints

  • 0 <= height.length <= 100,000
  • 0 <= height[i] <= 100,000

Follow-up Derive the two-pointer rule from the smaller of the left and right maxima.

Examples
Example 1
Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6
Example 2
Input: height = [4,2,0,3,2,5]
Output: 9
Example 3
Input: height = [1,0,1]
Output: 1
🔒 5 hidden
Running will execute all 8 cases, including 5 hidden ones.