0042. Trapping Rain Water

https://leetcode.com/problems/trapping-rain-water

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.

Example 1:

Example 2:

Constraints:

  • n == height.length

  • 1 <= n <= 2 * 104

  • 0 <= height[i] <= 105

ac1: two pointers

https://leetcode.com/problems/trapping-rain-water/discuss/17357/Sharing-my-simple-c%2B%2B-code%3A-O(n)-time-O(1)-space

ac2: brute force

ac3: DP-like memorization

ac4: stack

Very hard to understand, doubt if it's suitable for interview. https://leetcode.com/problems/trapping-rain-water/discuss/17414/A-stack-based-solution-for-reference-inspired-by-Histogram

Last updated

Was this helpful?