Skip to content
BASA DSA

Trapping Rain Water

HardTwo Pointers

Unit-width columns have the supplied nonnegative heights. Return the total area of rainwater retained between columns.

Example 1

Input
height = [4,1,2,0,3]
Output
6

Explanation

Trapped depths at indices 1,2,3 are 2,1,3, totaling 6.

Example 2

Input
height = [1,2,3]
Output
0

Explanation

An increasing skyline has no lower column bounded on both sides.

Example 3

Input
height = [2,0,2]
Output
2

Explanation

The height-two walls enclose two units above the central zero.

Constraints

  • 0 <= len(height) <= 20000
  • Heights are 0..100000

Function contracttrapArguments: json
Loading draft…
SolutionRuns on your device
Loading draft…