Skip to content

Commit bb4bd30

Browse files
committed
feat: update leetcode solutions: No.0011. Container With Most Water
1 parent 5b28619 commit bb4bd30

File tree

7 files changed

+274
-73
lines changed

7 files changed

+274
-73
lines changed

solution/0000-0099/0011.Container With Most Water/README.md

+111-1
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,132 @@
2626

2727
<!-- 这里可写通用的实现逻辑 -->
2828

29+
双指针解决。
30+
31+
一开始,我们考虑相距最远的两个柱子所能容纳水的面积。水的宽度是两根柱子之间的距离,而水的高度取决于两根柱子之间较短的那个。
32+
33+
- 当前柱子是最两侧的柱子,水的宽度最大,其他的组合,水的宽度都比这个小;
34+
- 当前左侧柱子较短,决定了水的高度。如果移动左侧的柱子,新的水面高度不确定,但一定不会超过右侧的柱子高度;
35+
- 如果移动右侧的柱子,新的水面高度一定不会超过左侧的柱子高度,也就是不会超过当前的水面高度。
36+
37+
可见,如果固定左侧的柱子,向内移动右侧的柱子,水的高度一定不会增加,且宽度一定减少,所以水的面积一定减少。所以左侧的柱子跟右侧其他柱子的组合,都可以排除了。也就是代码中的 `i++`
38+
39+
移动左侧的柱子中,重复进行上面的操作。
40+
41+
在此过程中,我们不断排除掉无法成为构成最大值的柱子组合,而每一次都获取到可能为最大值的面积 t。那么遍历结束之后,我们就可以得到最大值。
42+
2943
<!-- tabs:start -->
3044

3145
### **Python3**
3246

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

3549
```python
36-
50+
class Solution:
51+
def maxArea(self, height: List[int]) -> int:
52+
i, j = 0, len(height) - 1
53+
res = 0
54+
while i < j:
55+
t = (j - i) * min(height[i], height[j])
56+
res = max(res, t)
57+
if height[i] < height[j]:
58+
i += 1
59+
else:
60+
j -= 1
61+
return res
3762
```
3863

3964
### **Java**
4065

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

4368
```java
69+
class Solution {
70+
public int maxArea(int[] height) {
71+
int i = 0, j = height.length - 1;
72+
int res = 0;
73+
while (i < j) {
74+
int t = (j - i) * Math.min(height[i], height[j]);
75+
res = Math.max(res, t);
76+
if (height[i] < height[j]) ++i;
77+
else --j;
78+
}
79+
return res;
80+
}
81+
}
82+
```
83+
84+
### **C++**
85+
86+
```cpp
87+
class Solution {
88+
public:
89+
int maxArea(vector<int>& height) {
90+
int i = 0, j = height.size() - 1;
91+
int res = 0;
92+
while (i < j) {
93+
int t = (j - i) * min(height[i], height[j]);
94+
res = max(res, t);
95+
if (height[i] < height[j]) ++i;
96+
else --j;
97+
}
98+
return res;
99+
}
100+
};
101+
```
102+
103+
### **Go**
104+
105+
```go
106+
func maxArea(height []int) int {
107+
i, j := 0, len(height) - 1
108+
res := 0
109+
for i != j {
110+
t := (j - i) * min(height[i], height[j])
111+
res = max(res, t)
112+
if height[i] < height[j] {
113+
i++
114+
} else {
115+
j--
116+
}
117+
}
118+
return res
119+
}
120+
121+
func min(a, b int) int {
122+
if a > b {
123+
return b
124+
}
125+
return a
126+
}
127+
128+
func max(a, b int) int {
129+
if a > b {
130+
return a
131+
}
132+
return b
133+
}
134+
```
44135

136+
### **JavaScript**
137+
138+
```js
139+
/**
140+
* @param {number[]} height
141+
* @return {number}
142+
*/
143+
var maxArea = function (height) {
144+
let i = 0,
145+
j = height.length - 1;
146+
let res = 0;
147+
while (i < j) {
148+
const t = (j - i) * Math.min(height[i], height[j]);
149+
res = Math.max(res, t);
150+
if (height[i] < height[j]) ++i;
151+
else --j;
152+
}
153+
return res;
154+
};
45155
```
46156

47157
### **...**

solution/0000-0099/0011.Container With Most Water/README_EN.md

+97-1
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,109 @@
3131
### **Python3**
3232

3333
```python
34-
34+
class Solution:
35+
def maxArea(self, height: List[int]) -> int:
36+
i, j = 0, len(height) - 1
37+
res = 0
38+
while i < j:
39+
t = (j - i) * min(height[i], height[j])
40+
res = max(res, t)
41+
if height[i] < height[j]:
42+
i += 1
43+
else:
44+
j -= 1
45+
return res
3546
```
3647

3748
### **Java**
3849

3950
```java
51+
class Solution {
52+
public int maxArea(int[] height) {
53+
int i = 0, j = height.length - 1;
54+
int res = 0;
55+
while (i < j) {
56+
int t = (j - i) * Math.min(height[i], height[j]);
57+
res = Math.max(res, t);
58+
if (height[i] < height[j]) ++i;
59+
else --j;
60+
}
61+
return res;
62+
}
63+
}
64+
```
65+
66+
### **C++**
67+
68+
```cpp
69+
class Solution {
70+
public:
71+
int maxArea(vector<int>& height) {
72+
int i = 0, j = height.size() - 1;
73+
int res = 0;
74+
while (i < j) {
75+
int t = (j - i) * min(height[i], height[j]);
76+
res = max(res, t);
77+
if (height[i] < height[j]) ++i;
78+
else --j;
79+
}
80+
return res;
81+
}
82+
};
83+
```
84+
85+
### **Go**
86+
87+
```go
88+
func maxArea(height []int) int {
89+
i, j := 0, len(height) - 1
90+
res := 0
91+
for i != j {
92+
t := (j - i) * min(height[i], height[j])
93+
res = max(res, t)
94+
if height[i] < height[j] {
95+
i++
96+
} else {
97+
j--
98+
}
99+
}
100+
return res
101+
}
102+
103+
func min(a, b int) int {
104+
if a > b {
105+
return b
106+
}
107+
return a
108+
}
109+
110+
func max(a, b int) int {
111+
if a > b {
112+
return a
113+
}
114+
return b
115+
}
116+
```
40117

118+
### **JavaScript**
119+
120+
```js
121+
/**
122+
* @param {number[]} height
123+
* @return {number}
124+
*/
125+
var maxArea = function (height) {
126+
let i = 0,
127+
j = height.length - 1;
128+
let res = 0;
129+
while (i < j) {
130+
const t = (j - i) * Math.min(height[i], height[j]);
131+
res = Math.max(res, t);
132+
if (height[i] < height[j]) ++i;
133+
else --j;
134+
}
135+
return res;
136+
};
41137
```
42138

43139
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
class Solution {
22
public:
33
int maxArea(vector<int>& height) {
4-
int Max = -1, Min, area;
5-
int s = 0, e = height.size()-1;
6-
7-
while( s < e ){
8-
if( height[s] < height[e] ){
9-
area = (e-s) * height[s];
10-
s++;
11-
}
12-
else{
13-
area = (e-s) * height[e];
14-
e--;
15-
}
16-
if( area > Max ) Max = area;
4+
int i = 0, j = height.size() - 1;
5+
int res = 0;
6+
while (i < j) {
7+
int t = (j - i) * min(height[i], height[j]);
8+
res = max(res, t);
9+
if (height[i] < height[j]) ++i;
10+
else --j;
1711
}
18-
return Max;
12+
return res;
1913
}
20-
};
14+
};
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,28 @@
11
func maxArea(height []int) int {
2-
maxArea := 0
3-
i := 0
4-
j := len(height) - 1
5-
for i != j {
6-
hi := height[i]
7-
hj := height[j]
8-
maxArea = maxInt(maxArea, (j-i) * minInt(hi, hj))
9-
if hi >= hj {
10-
j--
11-
} else {
12-
i++
13-
}
14-
}
15-
return maxArea
2+
i, j := 0, len(height) - 1
3+
res := 0
4+
for i != j {
5+
t := (j - i) * min(height[i], height[j])
6+
res = max(res, t)
7+
if height[i] < height[j] {
8+
i++
9+
} else {
10+
j--
11+
}
12+
}
13+
return res
1614
}
1715

18-
func minInt(a, b int) int {
19-
if a >= b {
20-
return b
21-
}
22-
return a
16+
func min(a, b int) int {
17+
if a > b {
18+
return b
19+
}
20+
return a
2321
}
2422

25-
func maxInt(a, b int) int {
26-
if a >= b {
27-
return a
28-
}
29-
return b
23+
func max(a, b int) int {
24+
if a > b {
25+
return a
26+
}
27+
return b
3028
}
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
class Solution {
22
public int maxArea(int[] height) {
3-
int start = 0, end = height.length - 1, maxArea = 0;
4-
while (start < end) {
5-
int hs = height[start];
6-
int he = height[end];
7-
int l = end - start;
8-
if (hs > he) {
9-
maxArea = Math.max(he * l, maxArea);
10-
end--;
11-
} else {
12-
maxArea = Math.max(hs * l, maxArea);
13-
start++;
14-
}
3+
int i = 0, j = height.length - 1;
4+
int res = 0;
5+
while (i < j) {
6+
int t = (j - i) * Math.min(height[i], height[j]);
7+
res = Math.max(res, t);
8+
if (height[i] < height[j]) ++i;
9+
else --j;
1510
}
16-
return maxArea;
11+
return res;
1712
}
18-
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
1-
const maxArea2 = function (height) {
2-
let result = 0;
3-
for (let i = 0; i < height.length; i++) {
4-
for (let j = i + 1; j < height.length; j++) {
5-
result = Math.max(result, Math.min(height[i], height[j]) * (j - i));
6-
}
1+
/**
2+
* @param {number[]} height
3+
* @return {number}
4+
*/
5+
var maxArea = function (height) {
6+
let i = 0,
7+
j = height.length - 1;
8+
let res = 0;
9+
while (i < j) {
10+
const t = (j - i) * Math.min(height[i], height[j]);
11+
res = Math.max(res, t);
12+
if (height[i] < height[j]) ++i;
13+
else --j;
714
}
8-
return result;
9-
};
10-
11-
const maxArea = function (height) {
12-
let result = 0,
13-
l = 0,
14-
r = height.length - 1;
15-
while (l < r) {
16-
result = Math.max(result, Math.min(height[l], height[r]) * (r - l));
17-
height[l] < height[r] ? l++ : r--;
18-
}
19-
return result;
15+
return res;
2016
};

0 commit comments

Comments
 (0)