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 748dfb8

Browse files
committed
refactor solutions to container_with_most_water
1 parent 1d5bd93 commit 748dfb8

File tree

2 files changed

+7
-15
lines changed

2 files changed

+7
-15
lines changed

‎algs/0011.container_with_most_water.go‎

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,18 @@ MaxArea solves the following problem:
1414
*/
1515
func MaxArea(height []int) int {
1616
var (
17-
i, j = 0, len(height) - 1
18-
max = 0
17+
i, j = 0, len(height) - 1
18+
maxArea = 0
1919
)
2020
for j-i > 0 {
21+
area := min(height[i], height[j]) * (j - i)
22+
maxArea = max(maxArea, area)
2123
if height[j] < height[i] {
22-
area := height[j] * (j - i)
23-
if max < area {
24-
max = area
25-
}
2624
j--
2725
} else {
28-
area := height[i] * (j - i)
29-
if max < area {
30-
max = area
31-
}
3226
i++
3327
}
3428
}
3529

36-
return max
30+
return maxArea
3731
}

‎src/solution.rs‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,11 @@ impl Solution {
232232
let mut max_area = 0;
233233

234234
while j > i {
235+
let area = height[i].min(height[j]) * (j - i) as i32;
236+
max_area = max_area.max(area);
235237
if height[i] > height[j] {
236-
let area = height[j] * (j - i) as i32;
237-
max_area = max_area.max(area);
238238
j -= 1;
239239
} else {
240-
let area = height[i] * (j - i) as i32;
241-
max_area = max_area.max(area);
242240
i += 1;
243241
}
244242
}

0 commit comments

Comments
(0)

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