Skip to content

Commit a4ecfd8

Browse files
committed
feat: update leetcode/lcci solutions: No.0042. Trapping Rain Water
1 parent 6d6658c commit a4ecfd8

File tree

10 files changed

+259
-49
lines changed

10 files changed

+259
-49
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@
151151

152152
### 动态规划
153153

154+
- [接雨水](/solution/0000-0099/0042.Trapping%20Rain%20Water/README.md)
154155
- [最大子序和](/solution/0000-0099/0053.Maximum%20Subarray/README.md)
155156
- [乘积最大子序列](/solution/0100-0199/0152.Maximum%20Product%20Subarray/README.md)
156157
- [打家劫舍](/solution/0100-0199/0198.House%20Robber/README.md)

README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
145145

146146
### Dynamic Programming
147147

148+
- [Trapping Rain Water](/solution/0000-0099/0042.Trapping%20Rain%20Water/README_EN.md)
148149
- [Maximum Subarray](/solution/0000-0099/0053.Maximum%20Subarray/README_EN.md)
149150
- [Maximum Product Subarray](/solution/0100-0199/0152.Maximum%20Product%20Subarray/README_EN.md)
150151
- [House Robber](/solution/0100-0199/0198.House%20Robber/README_EN.md)

lcci/17.21.Volume of Histogram/README.md

+48-2
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,68 @@
2020

2121
<!-- 这里可写通用的实现逻辑 -->
2222

23+
动态规划法。
24+
25+
对于下标 i,水能达到的最大高度等于下标 i 左右两侧的最大高度的最小值,再减去 `height[i]` 就能得到当前柱子所能存的水量。
26+
27+
[42. 接雨水](/solution/0000-0099/0042.Trapping%20Rain%20Water/README.md)
28+
2329
<!-- tabs:start -->
2430

2531
### **Python3**
2632

2733
<!-- 这里可写当前语言的特殊实现逻辑 -->
2834

2935
```python
30-
36+
class Solution:
37+
def trap(self, height: List[int]) -> int:
38+
n = len(height)
39+
if n < 3:
40+
return 0
41+
42+
left_max = [height[0]] * n
43+
for i in range(1, n):
44+
left_max[i] = max(left_max[i - 1], height[i])
45+
46+
right_max = [height[n - 1]] * n
47+
for i in range(n - 2, -1, -1):
48+
right_max[i] = max(right_max[i + 1], height[i])
49+
50+
res = 0
51+
for i in range(n):
52+
res += min(left_max[i], right_max[i]) - height[i]
53+
return res
3154
```
3255

3356
### **Java**
3457

3558
<!-- 这里可写当前语言的特殊实现逻辑 -->
3659

3760
```java
38-
61+
class Solution {
62+
public int trap(int[] height) {
63+
int n;
64+
if ((n = height.length) < 3) return 0;
65+
66+
int[] leftMax = new int[n];
67+
leftMax[0] = height[0];
68+
for (int i = 1; i < n; ++i) {
69+
leftMax[i] = Math.max(leftMax[i - 1], height[i]);
70+
}
71+
72+
int[] rightMax = new int[n];
73+
rightMax[n - 1] = height[n - 1];
74+
for (int i = n - 2; i >= 0; --i) {
75+
rightMax[i] = Math.max(rightMax[i + 1], height[i]);
76+
}
77+
78+
int res = 0;
79+
for (int i = 0; i < n; ++i) {
80+
res += Math.min(leftMax[i], rightMax[i]) - height[i];
81+
}
82+
return res;
83+
}
84+
}
3985
```
4086

4187
### **...**

lcci/17.21.Volume of Histogram/README_EN.md

+42-2
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,53 @@
2525
### **Python3**
2626

2727
```python
28-
28+
class Solution:
29+
def trap(self, height: List[int]) -> int:
30+
n = len(height)
31+
if n < 3:
32+
return 0
33+
34+
left_max = [height[0]] * n
35+
for i in range(1, n):
36+
left_max[i] = max(left_max[i - 1], height[i])
37+
38+
right_max = [height[n - 1]] * n
39+
for i in range(n - 2, -1, -1):
40+
right_max[i] = max(right_max[i + 1], height[i])
41+
42+
res = 0
43+
for i in range(n):
44+
res += min(left_max[i], right_max[i]) - height[i]
45+
return res
2946
```
3047

3148
### **Java**
3249

3350
```java
34-
51+
class Solution {
52+
public int trap(int[] height) {
53+
int n;
54+
if ((n = height.length) < 3) return 0;
55+
56+
int[] leftMax = new int[n];
57+
leftMax[0] = height[0];
58+
for (int i = 1; i < n; ++i) {
59+
leftMax[i] = Math.max(leftMax[i - 1], height[i]);
60+
}
61+
62+
int[] rightMax = new int[n];
63+
rightMax[n - 1] = height[n - 1];
64+
for (int i = n - 2; i >= 0; --i) {
65+
rightMax[i] = Math.max(rightMax[i + 1], height[i]);
66+
}
67+
68+
int res = 0;
69+
for (int i = 0; i < n; ++i) {
70+
res += Math.min(leftMax[i], rightMax[i]) - height[i];
71+
}
72+
return res;
73+
}
74+
}
3575
```
3676

3777
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public int trap(int[] height) {
3+
int n;
4+
if ((n = height.length) < 3) return 0;
5+
6+
int[] leftMax = new int[n];
7+
leftMax[0] = height[0];
8+
for (int i = 1; i < n; ++i) {
9+
leftMax[i] = Math.max(leftMax[i - 1], height[i]);
10+
}
11+
12+
int[] rightMax = new int[n];
13+
rightMax[n - 1] = height[n - 1];
14+
for (int i = n - 2; i >= 0; --i) {
15+
rightMax[i] = Math.max(rightMax[i + 1], height[i]);
16+
}
17+
18+
int res = 0;
19+
for (int i = 0; i < n; ++i) {
20+
res += Math.min(leftMax[i], rightMax[i]) - height[i];
21+
}
22+
return res;
23+
}
24+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def trap(self, height: List[int]) -> int:
3+
n = len(height)
4+
if n < 3:
5+
return 0
6+
7+
left_max = [height[0]] * n
8+
for i in range(1, n):
9+
left_max[i] = max(left_max[i - 1], height[i])
10+
11+
right_max = [height[n - 1]] * n
12+
for i in range(n - 2, -1, -1):
13+
right_max[i] = max(right_max[i + 1], height[i])
14+
15+
res = 0
16+
for i in range(n):
17+
res += min(left_max[i], right_max[i]) - height[i]
18+
return res

solution/0000-0099/0042.Trapping Rain Water/README.md

+48-2
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,68 @@
2020

2121
<!-- 这里可写通用的实现逻辑 -->
2222

23+
动态规划法。
24+
25+
对于下标 i,水能达到的最大高度等于下标 i 左右两侧的最大高度的最小值,再减去 `height[i]` 就能得到当前柱子所能存的水量。
26+
27+
[面试题 17.21. 直方图的水量](/lcci/17.21.Volume%20of%20Histogram/README.md)
28+
2329
<!-- tabs:start -->
2430

2531
### **Python3**
2632

2733
<!-- 这里可写当前语言的特殊实现逻辑 -->
2834

2935
```python
30-
36+
class Solution:
37+
def trap(self, height: List[int]) -> int:
38+
n = len(height)
39+
if n < 3:
40+
return 0
41+
42+
left_max = [height[0]] * n
43+
for i in range(1, n):
44+
left_max[i] = max(left_max[i - 1], height[i])
45+
46+
right_max = [height[n - 1]] * n
47+
for i in range(n - 2, -1, -1):
48+
right_max[i] = max(right_max[i + 1], height[i])
49+
50+
res = 0
51+
for i in range(n):
52+
res += min(left_max[i], right_max[i]) - height[i]
53+
return res
3154
```
3255

3356
### **Java**
3457

3558
<!-- 这里可写当前语言的特殊实现逻辑 -->
3659

3760
```java
38-
61+
class Solution {
62+
public int trap(int[] height) {
63+
int n;
64+
if ((n = height.length) < 3) return 0;
65+
66+
int[] leftMax = new int[n];
67+
leftMax[0] = height[0];
68+
for (int i = 1; i < n; ++i) {
69+
leftMax[i] = Math.max(leftMax[i - 1], height[i]);
70+
}
71+
72+
int[] rightMax = new int[n];
73+
rightMax[n - 1] = height[n - 1];
74+
for (int i = n - 2; i >= 0; --i) {
75+
rightMax[i] = Math.max(rightMax[i + 1], height[i]);
76+
}
77+
78+
int res = 0;
79+
for (int i = 0; i < n; ++i) {
80+
res += Math.min(leftMax[i], rightMax[i]) - height[i];
81+
}
82+
return res;
83+
}
84+
}
3985
```
4086

4187
### **...**

solution/0000-0099/0042.Trapping Rain Water/README_EN.md

+42-2
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,53 @@
2525
### **Python3**
2626

2727
```python
28-
28+
class Solution:
29+
def trap(self, height: List[int]) -> int:
30+
n = len(height)
31+
if n < 3:
32+
return 0
33+
34+
left_max = [height[0]] * n
35+
for i in range(1, n):
36+
left_max[i] = max(left_max[i - 1], height[i])
37+
38+
right_max = [height[n - 1]] * n
39+
for i in range(n - 2, -1, -1):
40+
right_max[i] = max(right_max[i + 1], height[i])
41+
42+
res = 0
43+
for i in range(n):
44+
res += min(left_max[i], right_max[i]) - height[i]
45+
return res
2946
```
3047

3148
### **Java**
3249

3350
```java
34-
51+
class Solution {
52+
public int trap(int[] height) {
53+
int n;
54+
if ((n = height.length) < 3) return 0;
55+
56+
int[] leftMax = new int[n];
57+
leftMax[0] = height[0];
58+
for (int i = 1; i < n; ++i) {
59+
leftMax[i] = Math.max(leftMax[i - 1], height[i]);
60+
}
61+
62+
int[] rightMax = new int[n];
63+
rightMax[n - 1] = height[n - 1];
64+
for (int i = n - 2; i >= 0; --i) {
65+
rightMax[i] = Math.max(rightMax[i + 1], height[i]);
66+
}
67+
68+
int res = 0;
69+
for (int i = 0; i < n; ++i) {
70+
res += Math.min(leftMax[i], rightMax[i]) - height[i];
71+
}
72+
return res;
73+
}
74+
}
3575
```
3676

3777
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
class Solution {
22
public int trap(int[] height) {
3-
if (height == null || height.length == 0) return 0;
4-
int lx = 0, rx = height.length - 1, l = height[lx], r = height[rx], re = 0;
5-
while (lx < rx) {
6-
if (l < r) {
7-
lx++;
8-
if (height[lx] < l) re += l - height[lx];
9-
else l = height[lx];
10-
} else {
11-
rx--;
12-
if (height[rx] < r) re += r - height[rx];
13-
else r = height[rx];
14-
}
3+
int n;
4+
if ((n = height.length) < 3) return 0;
5+
6+
int[] leftMax = new int[n];
7+
leftMax[0] = height[0];
8+
for (int i = 1; i < n; ++i) {
9+
leftMax[i] = Math.max(leftMax[i - 1], height[i]);
1510
}
16-
return re;
11+
12+
int[] rightMax = new int[n];
13+
rightMax[n - 1] = height[n - 1];
14+
for (int i = n - 2; i >= 0; --i) {
15+
rightMax[i] = Math.max(rightMax[i + 1], height[i]);
16+
}
17+
18+
int res = 0;
19+
for (int i = 0; i < n; ++i) {
20+
res += Math.min(leftMax[i], rightMax[i]) - height[i];
21+
}
22+
return res;
1723
}
1824
}

0 commit comments

Comments
 (0)