Trapped rainwater
Hardarraytwo pointersdynamic programmingstack
heights[i] is the height of a bar of width 1. After rain, water is trapped between taller bars. Return how much water is trapped.
Example 1
Input: heights = [0,1,0,2,1,0,1,3,2,1,2,1] Output: 6
Example 2
Input: heights = [4,2,0,3,2,5] Output: 9
Constraints
- 0 ≤ heights.length ≤ 2·10⁴
- 0 ≤ heights[i] ≤ 10⁵
Submit checks your solution against 8 tests, including hidden ones.
/**
* @param {int[]} heights
* @return {int}
*/
function trap(heights) {
}