Skip to content

Commit 9bd13f2

Browse files
authored
fix: typo (#396)
1 parent f23916f commit 9bd13f2

File tree

2 files changed

+147
-48
lines changed

2 files changed

+147
-48
lines changed

problems/42.trapping-rain-water.en.md

+144-45
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
## Trapping Rain Water
2+
23
https://leetcode.com/problems/trapping-rain-water/description/
34

45
## Problem Description
6+
57
> Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.
68
79
![42.trapping-rain-water-1](../assets/problems/42.trapping-rain-water-1.png)
@@ -13,17 +15,26 @@ Input: [0,1,0,2,1,0,1,3,2,1,2,1]
1315
Output: 6
1416
```
1517

16-
## Solution
18+
## Prerequisites
19+
20+
- Space-time tradeoff
21+
- Two Pointers
22+
- Monotonic Stack
23+
24+
## Two Arrays
25+
26+
### Solution
1727

1828
The difficulty of this problem is `hard`.
1929
We'd like to compute how much water a given elevation map can trap.
2030

2131
A brute force solution would be adding up the maximum level of water that each element of the map can trap.
2232

2333
Pseudo Code:
34+
2435
```js
25-
for(let i = 0; i < height.length; i++) {
26-
area += h[i] - height[i]; // the maximum level of water that the element i can trap
36+
for (let i = 0; i < height.length; i++) {
37+
area += h[i] - height[i]; // the maximum level of water that the element i can trap
2738
}
2839
```
2940

@@ -34,16 +45,15 @@ For the given example, h would be [0, 1, 1, 2, 2, 2 ,2, 3, 2, 2, 2, 1].
3445

3546
The key is to calculate `leftMax` and `rightMax`.
3647

37-
## Key Points
48+
### Key Points
3849

3950
- Figure out the modeling of `h[i] = Math.min(leftMax, rightMax)`
4051

41-
## Code (JavaScript/Python3/C++)
52+
### Code (JavaScript/Python3/C++)
4253

4354
JavaScript Code:
4455

4556
```js
46-
4757
/*
4858
* @lc app=leetcode id=42 lang=javascript
4959
*
@@ -54,29 +64,28 @@ JavaScript Code:
5464
* @param {number[]} height
5565
* @return {number}
5666
*/
57-
var trap = function(height) {
58-
let max = 0;
59-
let volumn = 0;
60-
const leftMax = [];
61-
const rightMax = [];
62-
63-
for(let i = 0; i < height.length; i++) {
64-
leftMax[i] = max = Math.max(height[i], max);
65-
}
67+
var trap = function (height) {
68+
let max = 0;
69+
let volume = 0;
70+
const leftMax = [];
71+
const rightMax = [];
6672

67-
max = 0;
73+
for (let i = 0; i < height.length; i++) {
74+
leftMax[i] = max = Math.max(height[i], max);
75+
}
6876

69-
for(let i = height.length - 1; i >= 0; i--) {
70-
rightMax[i] = max = Math.max(height[i], max);
71-
}
77+
max = 0;
7278

73-
for(let i = 0; i < height.length; i++) {
74-
volumn = volumn + Math.min(leftMax[i], rightMax[i]) - height[i]
75-
}
79+
for (let i = height.length - 1; i >= 0; i--) {
80+
rightMax[i] = max = Math.max(height[i], max);
81+
}
7682

77-
return volumn;
78-
};
83+
for (let i = 0; i < height.length; i++) {
84+
volume = volume + Math.min(leftMax[i], rightMax[i]) - height[i];
85+
}
7986

87+
return volume;
88+
};
8089
```
8190

8291
Python Code:
@@ -93,38 +102,128 @@ class Solution:
93102
r[i] = max(r[i + 1], heights[i])
94103
for i in range(len(heights)):
95104
ans += max(0, min(l[i + 1], r[i]) - heights[i])
96-
return ans
105+
return ans
97106
```
98107

99108
C++ code:
100109

101110
```c++
111+
int trap(vector<int>& heights)
112+
{
113+
if(heights == null)
114+
return 0;
115+
int ans = 0;
116+
int size = heights.size();
117+
vector<int> left_max(size), right_max(size);
118+
left_max[0] = heights[0];
119+
for (int i = 1; i < size; i++) {
120+
left_max[i] = max(heights[i], left_max[i - 1]);
121+
}
122+
right_max[size - 1] = heights[size - 1];
123+
for (int i = size - 2; i >= 0; i--) {
124+
right_max[i] = max(heights[i], right_max[i + 1]);
125+
}
126+
for (int i = 1; i < size - 1; i++) {
127+
ans += min(left_max[i], right_max[i]) - heights[i];
128+
}
129+
return ans;
130+
}
131+
132+
```
133+
134+
**Complexity Analysis**
135+
136+
- Time Complexity: $O(N)$
137+
- Space Complexity: $O(N)$
138+
139+
## Two Pointers
140+
141+
### Solution
142+
143+
The above code is easy to understand, but it needs the extra space of N. We can tell from it that we in fact only cares about the minimum of (left[i], right[i]). Specifically:
144+
145+
- If l[i + 1] < r[i], the maximum in the left side of i will determine the height of trapping water.
146+
- If l[i + 1] >= r[i], the maximum in the right side of i will determine the height of trapping water.
147+
148+
Thus, we don't need to keep two complete arrays. We can rather keep only a left max and a right max, using constant variable. This problem is a typical two pointers problem.
149+
150+
Algorithm:
151+
152+
1. Initialize two pointers `left` and `right`, pointing to the begin and the end of our height array respectively.
153+
2. Initialize the left maximum height and the right maximum height to be 0.
154+
3. Compare height[left] and height[right]
155+
156+
- If height[left] < height[right]
157+
- 3.1.1 If height[left] >= left_max, the current trapping volume is (left_max - height[left])
158+
- 3.1.2 Otherwise, no water is trapped and the volume is 0
159+
- 3.2 Iterate the left pointer to the right
160+
- 3.3 If height[left] >= height[right]
161+
- 3.3.1 If height[right] >= right_max, the current trapping volume is (right_max - height[right])
162+
- 3.3.2 Otherwise, no water is trapped and the volume is 0
163+
- 3.4 Iterate the right pointer to the left
164+
165+
### Code (Python3/C++)
166+
167+
```python
168+
class Solution:
169+
def trap(self, heights: List[int]) -> int:
170+
n = len(heights)
171+
l_max = r_max = 0
172+
l, r = 0, n - 1
173+
ans = 0
174+
while l < r:
175+
if heights[l] < heights[r]:
176+
if heights[l] < l_max:
177+
ans += l_max - heights[l]
178+
else:
179+
l_max = heights[l]
180+
l += 1
181+
else:
182+
if heights[r] < r_max:
183+
ans += r_max - heights[r]
184+
else:
185+
r_max = heights[r]
186+
r -= 1
187+
return ans
188+
```
189+
190+
```c++
191+
102192
class Solution {
103193
public:
104-
int trap(vector<int>& height) {
105-
//check for empty input array
106-
if(height.empty())
107-
return 0;
108-
int size = height.size();
109-
int leftMax[size], rightMax[size];
110-
//initialization
111-
leftMax[0] = height[0];
112-
rightMax[size - 1] = height[size - 1];
113-
//find leftMax for each element i
114-
for(int i = 1; i < size; ++i)
115-
leftMax[i] = max(leftMax[i-1], height[i]);
116-
//find rightMax for each element i
117-
for(int i = size - 2; i >= 0; --i)
118-
rightMax[i] = max(rightMax[i+1], height[i]);
119-
//caculating the result
120-
int ans = 0;
121-
for(int i = 0; i < size; ++i)
122-
ans += min(leftMax[i], rightMax[i]) - height[i];
123-
return ans;
194+
int trap(vector<int>& heights)
195+
{
196+
int left = 0, right = heights.size() - 1;
197+
int ans = 0;
198+
int left_max = 0, right_max = 0;
199+
while (left < right) {
200+
if (heights[left] < heights[right]) {
201+
heights[left] >= left_max ? (left_max = heights[left]) : ans += (left_max - heights[left]);
202+
++left;
203+
}
204+
else {
205+
heights[right] >= right_max ? (right_max = heights[right]) : ans += (right_max - heights[right]);
206+
--right;
207+
}
124208
}
209+
return ans;
210+
}
211+
125212
};
126213
```
127214
215+
**Complexity Analysis**
216+
217+
- Time Complexity: $O(N)$
218+
- Space Complexity: $O(1)$
219+
128220
## Similar Problems
129221
130222
- [84.largest-rectangle-in-histogram](https://github.com/azl397985856/leetcode/blob/master/problems/84.largest-rectangle-in-histogram.md)
223+
224+
For more solutions, visit my [LeetCode Solution Repo](https://github.com/azl397985856/leetcode) (which has 30K stars).
225+
226+
Follow my WeChat official account 力扣加加, which has lots of graphic solutions and teaches you how to recognize problem patterns to solve problems with efficiency.
227+
228+
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg)
229+

problems/42.trapping-rain-water.md

100644100755
+3-3
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ JavaScript Code:
7777
*/
7878
var trap = function (height) {
7979
let max = 0;
80-
let volumn = 0;
80+
let volume = 0;
8181
const leftMax = [];
8282
const rightMax = [];
8383

@@ -92,10 +92,10 @@ var trap = function (height) {
9292
}
9393

9494
for (let i = 0; i < height.length; i++) {
95-
volumn = volumn + Math.min(leftMax[i], rightMax[i]) - height[i];
95+
volume = volume + Math.min(leftMax[i], rightMax[i]) - height[i];
9696
}
9797

98-
return volumn;
98+
return volume;
9999
};
100100
```
101101

0 commit comments

Comments
 (0)