Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7f10b62

Browse files
feat: update lc problems (#3892)
1 parent 9479b94 commit 7f10b62

File tree

43 files changed

+637
-456
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+637
-456
lines changed

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

Lines changed: 62 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,15 @@ tags:
6262

6363
### 方法一:双指针
6464

65-
一开始,我们考虑相距最远的两个柱子所能容纳水的容量。水的宽度是两根柱子之间的距离,而水的高度取决于两根柱子之间较短的那个
65+
我们使用两个指针 $l$ 和 $r$ 分别指向数组的左右两端,即 $l = 0,ドル而 $r = n - 1,ドル其中 $n$ 是数组的长度
6666

67-
当前柱子是最两侧的柱子,水的宽度最大,其他的组合,水的宽度都比这个小。不妨假设左侧柱子的高度小于等于右侧柱子的高度,那么水的高度就是左侧柱子的高度。如果我们移动右侧柱子,那么水的宽度就减小了,而水的高度却不会增加,因此水的容量一定减少。所以我们移动左侧柱子,更新最大容量
67+
接下来,我们使用变量 $\textit{ans}$ 记录容器的最大容量,初始化为 0ドル$
6868

69-
循环此过程,直到两个柱子相遇
69+
然后,我们开始进行循环,每次循环中,我们计算当前容器的容量,即 $\textit{min}(height[l], height[r]) \times (r - l),ドル并将其与 $\textit{ans}$ 进行比较,将较大值赋给 $\textit{ans}$。然后,我们判断 $height[l]$ 和 $height[r]$ 的大小,如果 $\textit{height}[l] < \textit{height}[r],ドル移动 $r$ 指针不会使得结果变得更好,因为容器的高度由较短的那根垂直线决定,所以我们移动 $l$ 指针。反之,我们移动 $r$ 指针
7070

71-
时间复杂度 $O(n),ドル其中 $n$ 是数组 `height` 的长度。空间复杂度 $O(1)$。
71+
遍历结束后,返回 $\textit{ans}$ 即可。
72+
73+
时间复杂度 $O(n),ドル其中 $n$ 是数组 $\textit{height}$ 的长度。空间复杂度 $O(1)$。
7274

7375
<!-- tabs:start -->
7476

@@ -77,15 +79,15 @@ tags:
7779
```python
7880
class Solution:
7981
def maxArea(self, height: List[int]) -> int:
80-
i, j = 0, len(height) - 1
82+
l, r = 0, len(height) - 1
8183
ans = 0
82-
while i < j:
83-
t = (j - i) *min(height[i], height[j])
84+
while l < r:
85+
t = min(height[l], height[r]) * (r - l)
8486
ans = max(ans, t)
85-
if height[i] < height[j]:
86-
i += 1
87+
if height[l] < height[r]:
88+
l += 1
8789
else:
88-
j -= 1
90+
r -= 1
8991
return ans
9092
```
9193

@@ -94,15 +96,15 @@ class Solution:
9496
```java
9597
class Solution {
9698
public int maxArea(int[] height) {
97-
int i = 0, j = height.length - 1;
99+
int l = 0, r = height.length - 1;
98100
int ans = 0;
99-
while (i < j) {
100-
int t = Math.min(height[i], height[j]) * (j - i);
101+
while (l < r) {
102+
int t = Math.min(height[l], height[r]) * (r - l);
101103
ans = Math.max(ans, t);
102-
if (height[i] < height[j]) {
103-
++i;
104+
if (height[l] < height[r]) {
105+
++l;
104106
} else {
105-
--j;
107+
--r;
106108
}
107109
}
108110
return ans;
@@ -116,15 +118,15 @@ class Solution {
116118
class Solution {
117119
public:
118120
int maxArea(vector<int>& height) {
119-
int i = 0, j = height.size() - 1;
121+
int l = 0, r = height.size() - 1;
120122
int ans = 0;
121-
while (i < j) {
122-
int t = min(height[i], height[j]) * (j - i);
123+
while (l < r) {
124+
int t = min(height[l], height[r]) * (r - l);
123125
ans = max(ans, t);
124-
if (height[i] < height[j]) {
125-
++i;
126+
if (height[l] < height[r]) {
127+
++l;
126128
} else {
127-
--j;
129+
--r;
128130
}
129131
}
130132
return ans;
@@ -136,14 +138,14 @@ public:
136138
137139
```go
138140
func maxArea(height []int) (ans int) {
139-
i, j := 0, len(height)-1
140-
for i < j {
141-
t := min(height[i], height[j]) * (j - i)
141+
l, r := 0, len(height)-1
142+
for l < r {
143+
t := min(height[l], height[r]) * (r - l)
142144
ans = max(ans, t)
143-
if height[i] < height[j] {
144-
i++
145+
if height[l] < height[r] {
146+
l++
145147
} else {
146-
j--
148+
r--
147149
}
148150
}
149151
return
@@ -154,16 +156,15 @@ func maxArea(height []int) (ans int) {
154156

155157
```ts
156158
function maxArea(height: number[]): number {
157-
let i = 0;
158-
let j = height.length - 1;
159+
let [l, r] = [0, height.length - 1];
159160
let ans = 0;
160-
while (i < j) {
161-
const t = Math.min(height[i], height[j]) * (j - i);
161+
while (l < r) {
162+
const t = Math.min(height[l], height[r]) * (r - l);
162163
ans = Math.max(ans, t);
163-
if (height[i] < height[j]) {
164-
++i;
164+
if (height[l] < height[r]) {
165+
++l;
165166
} else {
166-
--j;
167+
--r;
167168
}
168169
}
169170
return ans;
@@ -175,15 +176,15 @@ function maxArea(height: number[]): number {
175176
```rust
176177
impl Solution {
177178
pub fn max_area(height: Vec<i32>) -> i32 {
178-
let mut i = 0;
179-
let mut j = height.len() - 1;
179+
let mut l = 0;
180+
let mut r = height.len() - 1;
180181
let mut ans = 0;
181-
while i < j {
182-
ans = ans.max(height[i].min(height[j]) * ((j - i) as i32));
183-
if height[i] <= height[j] {
184-
i += 1;
182+
while l < r {
183+
ans = ans.max(height[l].min(height[r]) * ((r - l) as i32));
184+
if height[l] < height[r] {
185+
l += 1;
185186
} else {
186-
j -= 1;
187+
r -= 1;
187188
}
188189
}
189190
ans
@@ -199,16 +200,15 @@ impl Solution {
199200
* @return {number}
200201
*/
201202
var maxArea = function (height) {
202-
let i = 0;
203-
let j = height.length - 1;
203+
let [l, r] = [0, height.length - 1];
204204
let ans = 0;
205-
while (i < j) {
206-
const t = Math.min(height[i], height[j]) * (j - i);
205+
while (l < r) {
206+
const t = Math.min(height[l], height[r]) * (r - l);
207207
ans = Math.max(ans, t);
208-
if (height[i] < height[j]) {
209-
++i;
208+
if (height[l] < height[r]) {
209+
++l;
210210
} else {
211-
--j;
211+
--r;
212212
}
213213
}
214214
return ans;
@@ -220,15 +220,15 @@ var maxArea = function (height) {
220220
```cs
221221
public class Solution {
222222
public int MaxArea(int[] height) {
223-
int i = 0, j = height.Length - 1;
223+
int l = 0, r = height.Length - 1;
224224
int ans = 0;
225-
while (i < j) {
226-
int t = Math.Min(height[i], height[j]) * (j - i);
225+
while (l < r) {
226+
int t = Math.Min(height[l], height[r]) * (r - l);
227227
ans = Math.Max(ans, t);
228-
if (height[i] < height[j]) {
229-
++i;
228+
if (height[l] < height[r]) {
229+
++l;
230230
} else {
231-
--j;
231+
--r;
232232
}
233233
}
234234
return ans;
@@ -245,16 +245,16 @@ class Solution {
245245
* @return Integer
246246
*/
247247
function maxArea($height) {
248-
$i = 0;
249-
$j = count($height) - 1;
248+
$l = 0;
249+
$r = count($height) - 1;
250250
$ans = 0;
251-
while ($i < $j) {
252-
$t = min($height[$i], $height[$j]) * ($j - $i);
251+
while ($l < $r) {
252+
$t = min($height[$l], $height[$r]) * ($r - $l);
253253
$ans = max($ans, $t);
254-
if ($height[$i] < $height[$j]) {
255-
++$i;
254+
if ($height[$l] < $height[$r]) {
255+
++$l;
256256
} else {
257-
--$j;
257+
--$r;
258258
}
259259
}
260260
return $ans;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /