 All Problems
Trapping Rain Water
hard
arrays
two pointers
stack
amazon
google
facebook
microsoft

Given n non-negative integers representing an elevation map, compute how much water it can trap after raining.

Example 1:

Input: 0 1 0 2 1 0 1 3 2 1 2 1
Output: 6

Example 2:

Input: 4 2 0 3 2 5
Output: 9

Constraints:

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

Input format: Space-separated heights.

Output format: Total water trapped.

Run to check your code against the sample cases, or submit to run every case