Skip to content

Commit fbcbec3

Browse files
committed
feat: add solutions to lc problem: No.0042
No.0042.Trapping Rain Water
1 parent 2501424 commit fbcbec3

File tree

7 files changed

+143
-242
lines changed

7 files changed

+143
-242
lines changed

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

+53-86
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@
4141

4242
<!-- 这里可写通用的实现逻辑 -->
4343

44-
动态规划法。
44+
**方法一:动态规划**
4545

46-
对于下标 i,水能达到的最大高度等于下标 i 左右两侧的最大高度的最小值,再减去 `height[i]` 就能得到当前柱子所能存的水量
46+
我们定义 $left[i]$ 表示下标 $i$ 位置及其左边的最高柱子的高度,定义 $right[i]$ 表示下标 $i$ 位置及其右边的最高柱子的高度。那么下标 $i$ 位置能接的雨水量为 $min(left[i], right[i]) - height[i]$。我们遍历数组,计算出 $left[i]$ 和 $right[i]$,最后答案为 $\sum_{i=0}^{n-1} min(left[i], right[i]) - height[i]$
4747

48-
[面试题 17.21. 直方图的水量](/lcci/17.21.Volume%20of%20Histogram/README.md)
48+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组的长度。
4949

5050
<!-- tabs:start -->
5151

@@ -57,18 +57,12 @@
5757
class Solution:
5858
def trap(self, height: List[int]) -> int:
5959
n = len(height)
60-
if n < 3:
61-
return 0
62-
63-
lmx, rmx = [height[0]] * n, [height[n - 1]] * n
60+
left = [height[0]] * n
61+
right = [height[-1]] * n
6462
for i in range(1, n):
65-
lmx[i] = max(lmx[i - 1], height[i])
66-
rmx[n - 1 - i] = max(rmx[n - i], height[n - 1 - i])
67-
68-
res = 0
69-
for i in range(n):
70-
res += min(lmx[i], rmx[i]) - height[i]
71-
return res
63+
left[i] = max(left[i - 1], height[i])
64+
right[n - i - 1] = max(right[n - i], height[n - i - 1])
65+
return sum(min(l, r) - h for l, r, h in zip(left, right, height))
7266
```
7367

7468
### **Java**
@@ -79,108 +73,62 @@ class Solution:
7973
class Solution {
8074
public int trap(int[] height) {
8175
int n = height.length;
82-
if (n < 3) {
83-
return 0;
84-
}
85-
86-
int[] lmx = new int[n];
87-
int[] rmx = new int[n];
88-
lmx[0] = height[0];
89-
rmx[n - 1] = height[n - 1];
76+
int[] left = new int[n];
77+
int[] right = new int[n];
78+
left[0] = height[0];
79+
right[n - 1] = height[n - 1];
9080
for (int i = 1; i < n; ++i) {
91-
lmx[i] = Math.max(lmx[i - 1], height[i]);
92-
rmx[n - 1 - i] = Math.max(rmx[n - i], height[n - i - 1]);
81+
left[i] = Math.max(left[i - 1], height[i]);
82+
right[n - i - 1] = Math.max(right[n - i], height[n - i - 1]);
9383
}
94-
95-
int res = 0;
84+
int ans = 0;
9685
for (int i = 0; i < n; ++i) {
97-
res += Math.min(lmx[i], rmx[i]) - height[i];
86+
ans += Math.min(left[i], right[i]) - height[i];
9887
}
99-
return res;
88+
return ans;
10089
}
10190
}
10291
```
10392

104-
### **TypeScript**
105-
106-
```ts
107-
function trap(height: number[]): number {
108-
let ans = 0;
109-
let left = 0,
110-
right = height.length - 1;
111-
let maxLeft = 0,
112-
maxRight = 0;
113-
while (left < right) {
114-
if (height[left] < height[right]) {
115-
// move left
116-
if (height[left] >= maxLeft) {
117-
maxLeft = height[left];
118-
} else {
119-
ans += maxLeft - height[left];
120-
}
121-
++left;
122-
} else {
123-
// move right
124-
if (height[right] >= maxRight) {
125-
maxRight = height[right];
126-
} else {
127-
ans += maxRight - height[right];
128-
}
129-
--right;
130-
}
131-
}
132-
return ans;
133-
}
134-
```
135-
13693
### **C++**
13794

13895
```cpp
13996
class Solution {
14097
public:
14198
int trap(vector<int>& height) {
14299
int n = height.size();
143-
if (n < 3) {
144-
return 0;
145-
}
146-
147-
vector<int> lmx(n, height[0]);
148-
vector<int> rmx(n, height[n - 1]);
100+
int left[n], right[n];
101+
left[0] = height[0];
102+
right[n - 1] = height[n - 1];
149103
for (int i = 1; i < n; ++i) {
150-
lmx[i] = max(lmx[i - 1], height[i]);
151-
rmx[n - 1 - i] = max(rmx[n - i], height[n - 1 - i]);
104+
left[i] = max(left[i - 1], height[i]);
105+
right[n - i - 1] = max(right[n - i], height[n - i - 1]);
152106
}
153-
154-
int res = 0;
107+
int ans = 0;
155108
for (int i = 0; i < n; ++i) {
156-
res += min(lmx[i], rmx[i]) - height[i];
109+
ans += min(left[i], right[i]) - height[i];
157110
}
158-
return res;
111+
return ans;
159112
}
160113
};
161114
```
162115
163116
### **Go**
164117
165118
```go
166-
func trap(height []int) int {
119+
func trap(height []int) (ans int) {
167120
n := len(height)
168-
if n < 3 {
169-
return 0
170-
}
171-
172-
lmx, rmx := make([]int, n), make([]int, n)
173-
lmx[0], rmx[n-1] = height[0], height[n-1]
121+
left := make([]int, n)
122+
right := make([]int, n)
123+
left[0], right[n-1] = height[0], height[n-1]
174124
for i := 1; i < n; i++ {
175-
lmx[i] = max(lmx[i-1], height[i])
176-
rmx[n-1-i] = max(rmx[n-i], height[n-1-i])
125+
left[i] = max(left[i-1], height[i])
126+
right[n-i-1] = max(right[n-i], height[n-i-1])
177127
}
178-
179-
res := 0
180-
for i := 0; i < n; i++ {
181-
res += min(lmx[i], rmx[i]) - height[i]
128+
for i, h := range height {
129+
ans += min(left[i], right[i]) - h
182130
}
183-
return res
131+
return
184132
}
185133
186134
func max(a, b int) int {
@@ -198,6 +146,25 @@ func min(a, b int) int {
198146
}
199147
```
200148

149+
### **TypeScript**
150+
151+
```ts
152+
function trap(height: number[]): number {
153+
const n = height.length;
154+
const left: number[] = new Array(n).fill(height[0]);
155+
const right: number[] = new Array(n).fill(height[n - 1]);
156+
for (let i = 1; i < n; ++i) {
157+
left[i] = Math.max(left[i - 1], height[i]);
158+
right[n - i - 1] = Math.max(right[n - i], height[n - i - 1]);
159+
}
160+
let ans = 0;
161+
for (let i = 0; i < n; ++i) {
162+
ans += Math.min(left[i], right[i]) - height[i];
163+
}
164+
return ans;
165+
}
166+
```
167+
201168
### **...**
202169

203170
```

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

+50-83
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,12 @@
4141
class Solution:
4242
def trap(self, height: List[int]) -> int:
4343
n = len(height)
44-
if n < 3:
45-
return 0
46-
47-
lmx, rmx = [height[0]] * n, [height[n - 1]] * n
44+
left = [height[0]] * n
45+
right = [height[-1]] * n
4846
for i in range(1, n):
49-
lmx[i] = max(lmx[i - 1], height[i])
50-
rmx[n - 1 - i] = max(rmx[n - i], height[n - 1 - i])
51-
52-
res = 0
53-
for i in range(n):
54-
res += min(lmx[i], rmx[i]) - height[i]
55-
return res
47+
left[i] = max(left[i - 1], height[i])
48+
right[n - i - 1] = max(right[n - i], height[n - i - 1])
49+
return sum(min(l, r) - h for l, r, h in zip(left, right, height))
5650
```
5751

5852
### **Java**
@@ -61,108 +55,62 @@ class Solution:
6155
class Solution {
6256
public int trap(int[] height) {
6357
int n = height.length;
64-
if (n < 3) {
65-
return 0;
66-
}
67-
68-
int[] lmx = new int[n];
69-
int[] rmx = new int[n];
70-
lmx[0] = height[0];
71-
rmx[n - 1] = height[n - 1];
58+
int[] left = new int[n];
59+
int[] right = new int[n];
60+
left[0] = height[0];
61+
right[n - 1] = height[n - 1];
7262
for (int i = 1; i < n; ++i) {
73-
lmx[i] = Math.max(lmx[i - 1], height[i]);
74-
rmx[n - 1 - i] = Math.max(rmx[n - i], height[n - i - 1]);
63+
left[i] = Math.max(left[i - 1], height[i]);
64+
right[n - i - 1] = Math.max(right[n - i], height[n - i - 1]);
7565
}
76-
77-
int res = 0;
66+
int ans = 0;
7867
for (int i = 0; i < n; ++i) {
79-
res += Math.min(lmx[i], rmx[i]) - height[i];
68+
ans += Math.min(left[i], right[i]) - height[i];
8069
}
81-
return res;
70+
return ans;
8271
}
8372
}
8473
```
8574

86-
### **TypeScript**
87-
88-
```ts
89-
function trap(height: number[]): number {
90-
let ans = 0;
91-
let left = 0,
92-
right = height.length - 1;
93-
let maxLeft = 0,
94-
maxRight = 0;
95-
while (left < right) {
96-
if (height[left] < height[right]) {
97-
// move left
98-
if (height[left] >= maxLeft) {
99-
maxLeft = height[left];
100-
} else {
101-
ans += maxLeft - height[left];
102-
}
103-
++left;
104-
} else {
105-
// move right
106-
if (height[right] >= maxRight) {
107-
maxRight = height[right];
108-
} else {
109-
ans += maxRight - height[right];
110-
}
111-
--right;
112-
}
113-
}
114-
return ans;
115-
}
116-
```
117-
11875
### **C++**
11976

12077
```cpp
12178
class Solution {
12279
public:
12380
int trap(vector<int>& height) {
12481
int n = height.size();
125-
if (n < 3) {
126-
return 0;
127-
}
128-
129-
vector<int> lmx(n, height[0]);
130-
vector<int> rmx(n, height[n - 1]);
82+
int left[n], right[n];
83+
left[0] = height[0];
84+
right[n - 1] = height[n - 1];
13185
for (int i = 1; i < n; ++i) {
132-
lmx[i] = max(lmx[i - 1], height[i]);
133-
rmx[n - 1 - i] = max(rmx[n - i], height[n - 1 - i]);
86+
left[i] = max(left[i - 1], height[i]);
87+
right[n - i - 1] = max(right[n - i], height[n - i - 1]);
13488
}
135-
136-
int res = 0;
89+
int ans = 0;
13790
for (int i = 0; i < n; ++i) {
138-
res += min(lmx[i], rmx[i]) - height[i];
91+
ans += min(left[i], right[i]) - height[i];
13992
}
140-
return res;
93+
return ans;
14194
}
14295
};
14396
```
14497
14598
### **Go**
14699
147100
```go
148-
func trap(height []int) int {
101+
func trap(height []int) (ans int) {
149102
n := len(height)
150-
if n < 3 {
151-
return 0
152-
}
153-
154-
lmx, rmx := make([]int, n), make([]int, n)
155-
lmx[0], rmx[n-1] = height[0], height[n-1]
103+
left := make([]int, n)
104+
right := make([]int, n)
105+
left[0], right[n-1] = height[0], height[n-1]
156106
for i := 1; i < n; i++ {
157-
lmx[i] = max(lmx[i-1], height[i])
158-
rmx[n-1-i] = max(rmx[n-i], height[n-1-i])
107+
left[i] = max(left[i-1], height[i])
108+
right[n-i-1] = max(right[n-i], height[n-i-1])
159109
}
160-
161-
res := 0
162-
for i := 0; i < n; i++ {
163-
res += min(lmx[i], rmx[i]) - height[i]
110+
for i, h := range height {
111+
ans += min(left[i], right[i]) - h
164112
}
165-
return res
113+
return
166114
}
167115
168116
func max(a, b int) int {
@@ -180,6 +128,25 @@ func min(a, b int) int {
180128
}
181129
```
182130

131+
### **TypeScript**
132+
133+
```ts
134+
function trap(height: number[]): number {
135+
const n = height.length;
136+
const left: number[] = new Array(n).fill(height[0]);
137+
const right: number[] = new Array(n).fill(height[n - 1]);
138+
for (let i = 1; i < n; ++i) {
139+
left[i] = Math.max(left[i - 1], height[i]);
140+
right[n - i - 1] = Math.max(right[n - i], height[n - i - 1]);
141+
}
142+
let ans = 0;
143+
for (let i = 0; i < n; ++i) {
144+
ans += Math.min(left[i], right[i]) - height[i];
145+
}
146+
return ans;
147+
}
148+
```
149+
183150
### **...**
184151

185152
```

0 commit comments

Comments
 (0)