|
| 1 | +--- |
| 2 | +id: trapping-rain-water |
| 3 | +title: Trapping Rain Water(LeetCode) |
| 4 | +sidebar_label: 0042-Trapping Rain Water |
| 5 | +tags: |
| 6 | + - Array |
| 7 | + - Two Pointers |
| 8 | + - Dynamic Programming |
| 9 | + - Stack |
| 10 | + - Monotonic Stack |
| 11 | +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. |
| 12 | +sidebar_position: 42 |
| 13 | +--- |
| 14 | + |
| 15 | +## Problem Statement |
| 16 | + |
| 17 | +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. |
| 18 | + |
| 19 | +### Examples |
| 20 | + |
| 21 | +**Example 1:** |
| 22 | + |
| 23 | + |
| 24 | +```plaintext |
| 25 | +Input: height = [0,1,0,2,1,0,1,3,2,1,2,1] |
| 26 | +Output: 6 |
| 27 | +Explanation: The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. |
| 28 | +In this case, 6 units of rain water (blue section) are being trapped. |
| 29 | +``` |
| 30 | + |
| 31 | +**Example 2:** |
| 32 | + |
| 33 | +```plaintext |
| 34 | +Input: height = [4,2,0,3,2,5] |
| 35 | +Output: 9 |
| 36 | +``` |
| 37 | + |
| 38 | +### Constraints |
| 39 | + |
| 40 | +- `n == height.length` |
| 41 | +- `1 <= n <= 2 * 104` |
| 42 | +- `0 <= height[i] <= 105` |
| 43 | + |
| 44 | +## Solution |
| 45 | + |
| 46 | +The Trapping Rain Water problem involves calculating how much water can be trapped between bars after raining, |
| 47 | +given an array representing the elevation map. |
| 48 | + |
| 49 | +### Approach : Two-Pointer Technique |
| 50 | + |
| 51 | +This approach uses two pointers to traverse the height array from both ends, maintaining the maximum height encountered from the left and the right. |
| 52 | +The trapped water is calculated based on the minimum of these maximum heights. |
| 53 | + |
| 54 | +#### Explanation: |
| 55 | + |
| 56 | +1. Initialize two pointers: `left` at the start of the array and `right` at the end. |
| 57 | +2. Maintain two variables: `leftMax` for the maximum height encountered from the left and `rightMax` for the maximum height from the right. |
| 58 | +3. Iterate through the array using the two pointers: |
| 59 | +4. If the height at the left pointer is less than the height at the right pointer: |
| 60 | +* Move the left pointer to the right. |
| 61 | +* Update `leftMax` if the new height is greater than `leftMax`. |
| 62 | +* Calculate trapped water at the left pointer if the new height is less than `leftMax`. |
| 63 | +5. If the height at the right pointer is less than or equal to the height at the left pointer: |
| 64 | +* Move the right pointer to the left. |
| 65 | +* Update `rightMax` if the new height is greater than `rightMax`. |
| 66 | +* Calculate trapped water at the right pointer if the new height is less than `rightMax`. |
| 67 | +6. Continue this process until the left and right pointers meet. |
| 68 | +7. The total trapped water is the sum of water calculated at each step. |
| 69 | + |
| 70 | +#### Algorithm |
| 71 | + |
| 72 | +1. Initialize `left` at 0, `right` at the end of the array. |
| 73 | +2. Set `leftMax` to the first element, `rightMax` to the last element. |
| 74 | +3. While `left` is less than `right`: |
| 75 | +4. If `leftMax` is less than `rightMax`: |
| 76 | +* Increment `left`. |
| 77 | +* Update `leftMax` if the new height is greater. |
| 78 | +* Add the difference between `leftMax` and the current height to the water. |
| 79 | +4. Else: |
| 80 | +* Decrement `right`. |
| 81 | +* Update `rightMax` if the new height is greater. |
| 82 | +* Add the difference between `rightMax` and the current height to the water. |
| 83 | +5. Return the total trapped water. |
| 84 | + |
| 85 | +#### Implementation |
| 86 | + |
| 87 | +C++ Solution: |
| 88 | + |
| 89 | +```C++ |
| 90 | +class Solution { |
| 91 | +public: |
| 92 | + int trap(vector<int>& height) { |
| 93 | + int n = height.size(); |
| 94 | + int lmax = height[0]; |
| 95 | + int rmax = height[n-1]; |
| 96 | + int lpos = 1; |
| 97 | + int rpos = n-2; |
| 98 | + int water = 0; |
| 99 | + while (lpos <= rpos) { |
| 100 | + if (height[lpos] >= lmax) { |
| 101 | + lmax = height[lpos]; |
| 102 | + lpos++; |
| 103 | + } else if (height[rpos] >= rmax) { |
| 104 | + rmax = height[rpos]; |
| 105 | + rpos--; |
| 106 | + } else if (lmax <= rmax && height[lpos] < lmax) { |
| 107 | + water += lmax - height[lpos]; |
| 108 | + lpos++; |
| 109 | + } else { |
| 110 | + water += rmax - height[rpos]; |
| 111 | + rpos--; |
| 112 | + } |
| 113 | + } |
| 114 | + return water; |
| 115 | + } |
| 116 | +}; |
| 117 | +``` |
| 118 | +
|
| 119 | +Java Solution: |
| 120 | +
|
| 121 | +```Java |
| 122 | +class Solution { |
| 123 | + public int trap(int[] height) { |
| 124 | + int left = 0, right = height.length - 1; |
| 125 | + int leftMax = height[0], rightMax = height[height.length - 1]; |
| 126 | + int water = 0; |
| 127 | + while (left < right) { |
| 128 | + if (leftMax < rightMax) { |
| 129 | + left++; |
| 130 | + if (leftMax < height[left]) { |
| 131 | + leftMax = height[left]; |
| 132 | + } else { |
| 133 | + water += leftMax - height[left]; |
| 134 | + } |
| 135 | + } else { |
| 136 | + right--; |
| 137 | + if (rightMax < height[right]) { |
| 138 | + rightMax = height[right]; |
| 139 | + } else { |
| 140 | + water += rightMax - height[right]; |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + return water; |
| 145 | + } |
| 146 | +} |
| 147 | +``` |
| 148 | + |
| 149 | +Python Solution: |
| 150 | + |
| 151 | +```Python |
| 152 | +class Solution: |
| 153 | + def sumBackets(self, height: list[int], left, right): |
| 154 | + minHeightLeft = height[left] |
| 155 | + total = 0 |
| 156 | + leftBacket = 0 |
| 157 | + locationMinLeft = left |
| 158 | + |
| 159 | + while left < right: |
| 160 | + if height[left] < minHeightLeft: |
| 161 | + leftBacket += minHeightLeft - height[left] |
| 162 | + else: |
| 163 | + minHeightLeft = height[left] |
| 164 | + total += leftBacket |
| 165 | + leftBacket = 0 |
| 166 | + locationMinLeft = left |
| 167 | + left += 1 |
| 168 | + |
| 169 | + if minHeightLeft <= height[right]: |
| 170 | + return total + leftBacket, right |
| 171 | + else: |
| 172 | + return total, locationMinLeft |
| 173 | + |
| 174 | + def sumBacketsReverce(self, height: list[int], left, right): |
| 175 | + minHeightRight = height[right] |
| 176 | + total = 0 |
| 177 | + rightBacket = 0 |
| 178 | + locationMinRight = right |
| 179 | + |
| 180 | + while left < right: |
| 181 | + if height[right] < minHeightRight: |
| 182 | + rightBacket += minHeightRight - height[right] |
| 183 | + else: |
| 184 | + minHeightRight = height[right] |
| 185 | + total += rightBacket |
| 186 | + rightBacket = 0 |
| 187 | + locationMinRight = right |
| 188 | + right -= 1 |
| 189 | + |
| 190 | + if minHeightRight <= height[left]: |
| 191 | + return total + rightBacket, left |
| 192 | + else: |
| 193 | + return total, locationMinRight |
| 194 | + |
| 195 | + def trap(self, height: List[int]) -> int: |
| 196 | + right = len(height) - 1 |
| 197 | + left = 0 |
| 198 | + totalSum = 0 |
| 199 | + |
| 200 | + while left < right - 1: |
| 201 | + if height[left] < height[right]: |
| 202 | + total, left = self.sumBackets(height, left, right) |
| 203 | + else: |
| 204 | + total, right = self.sumBacketsReverce(height, left, right) |
| 205 | + totalSum += total |
| 206 | + |
| 207 | + return totalSum |
| 208 | +``` |
| 209 | +### Complexity Analysis |
| 210 | + |
| 211 | +- **Time complexity**: O(n), where n is the number of elements in the height array. The array is traversed once. |
| 212 | +- **Space complexity**: O(1), as no extra space is used except for variables. |
0 commit comments