Trapping Rain Water

Hard Solved

Description

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining.

Input format / Clarification:

  • Line 1: JSON array of integers representing heights

Examples

Input:

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

Output: 6

Input:

[4,2,0,3,2,5]

Output: 9

Input:

[1,2,3,4]

Output: 0

Note:

Your program must print a single integer representing the total trapped water so it can be compared with the expected output.

No submissions yet.

Discuss brute-force, prefix max arrays, two-pointer optimization, and stack-based approaches.

Test Cases