Problem · Array

Trapping Rain Water

Learn this problem
MediumZolostaysINTERNOA

Problem statement

Given n non-negative integers representing an elevation map where each bar has width 1, return the total amount of rain water trapped after raining.

Function

trap(height: int[]) → int

Examples

Example 1

height = [0,1,0,2,1,0,1,3,2,1,2,1]return = 6

The elevation map traps 6 units of water in total.

Example 2

height = [4,2,0,3,2,5]return = 9

The bars trap 9 units of water in total.

Constraints

  • n == height.length
  • 1 <= n <= 2 * 10^4
  • 0 <= height[i] <= 10^5

More Zolostays problems

drafts saved locally
public int trap(int[] height) {
  // write your code here
}
height[0,1,0,2,1,0,1,3,2,1,2,1]
expected6
checking account