|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: easy |
| 4 | +# Follow `Topics` tags |
| 5 | +tags: |
| 6 | + - Array |
| 7 | + - Two Pointers |
| 8 | + - Dynamic Programming |
| 9 | + - Stack |
| 10 | + - Monotonic Stack |
| 11 | +--- |
| 12 | + |
| 13 | +# [42. Trapping Rain Water](https://leetcode.com/problems/trapping-rain-water/description/) |
| 14 | + |
| 15 | +## Description |
| 16 | + |
| 17 | +Given an elevation map with width of 1 which representing by `n` non-negative integers. Calculate amount of water that can be trapped after rainfall. |
| 18 | + |
| 19 | +**Example 1:** |
| 20 | +``` |
| 21 | +Input: height = [0,1,0,0,1,0,1,3,2,0,1] |
| 22 | +Output: 4 |
| 23 | +Explanation: [1,0,0,1] can trapped 2 units, [1,0,1] can trapped 1 units, [2,0,1] can trapped 1 units. |
| 24 | +``` |
| 25 | + |
| 26 | +**Example 2:** |
| 27 | +``` |
| 28 | +Input: height = [4,1,2,4,5] |
| 29 | +Output: 5 |
| 30 | +Explanation: [4,1,2,4] can trapped 5 units. |
| 31 | +``` |
| 32 | + |
| 33 | + |
| 34 | +**Constraints:** |
| 35 | +`n == height.length` |
| 36 | +`1 <= n <= 2 * 10^4` |
| 37 | +`0 <= height[i] <= 10^5` |
| 38 | + |
| 39 | +## Solution |
| 40 | + |
| 41 | +Shrink from the border. If the outside is higher than the inside, rain will accumulate. Therefore, the difference between the higher and lower bars represents the amount of retained rainwater. |
| 42 | + |
| 43 | + |
| 44 | +```java |
| 45 | +class Solution { |
| 46 | + public int trap(int[] height) { |
| 47 | + int left = 0, right = height.length - 1; |
| 48 | + int leftMax = height[left], rightMax = height[right]; |
| 49 | + int water = 0; |
| 50 | + |
| 51 | + while (left < right) { |
| 52 | + if (leftMax < rightMax) { |
| 53 | + left++; |
| 54 | + leftMax = Math.max(leftMax, height[left]); |
| 55 | + water += leftMax - height[left]; |
| 56 | + } else { |
| 57 | + right--; |
| 58 | + rightMax = Math.max(rightMax, height[right]); |
| 59 | + water += rightMax - height[right]; |
| 60 | + } |
| 61 | + } |
| 62 | + return water; |
| 63 | + } |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +```python |
| 68 | +class Solution: |
| 69 | + def trap(self, height: list[int]) -> int: |
| 70 | + if not height: |
| 71 | + return 0 |
| 72 | + |
| 73 | + left = 0 |
| 74 | + right = len(height) - 1 |
| 75 | + left_max = height[left] |
| 76 | + right_max = height[right] |
| 77 | + water = 0 |
| 78 | + |
| 79 | + while left < right: |
| 80 | + if left_max < right_max: |
| 81 | + left += 1 |
| 82 | + left_max = max(left_max, height[left]) |
| 83 | + water += left_max - height[left] |
| 84 | + else: |
| 85 | + right -= 1 |
| 86 | + right_max = max(right_max, height[right]) |
| 87 | + water += right_max - height[right] |
| 88 | + return water |
| 89 | +``` |
| 90 | + |
| 91 | +## Complexity |
| 92 | + |
| 93 | +- Time complexity: $$O(n)$$ |
| 94 | +<!-- Add time complexity here, e.g. $$O(n)$$ --> |
| 95 | + |
| 96 | +- Space complexity: $$O(1)$$ |
| 97 | +<!-- Add space complexity here, e.g. $$O(n)$$ --> |
0 commit comments