Skip to content

Commit 1765771

Browse files
author
zim0369
committed
Update 11-Container-With-Most-Water.rs
1 parent 939af48 commit 1765771

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed
Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
11
impl Solution {
22
pub fn max_area(height: Vec<i32>) -> i32 {
3-
let (mut l, mut r) = (0, height.len() - 1);
4-
5-
let mut max = 0;
6-
7-
while (l < r){
8-
let (lh, rh) = (height[l], height[r]);
9-
let h = lh.min(rh);
10-
11-
let d = (r - l) as i32;
12-
let area = d * h;
13-
14-
if area > max{
15-
max = area;
16-
}
17-
18-
if rh < lh{
19-
while r > 0 && height[r] <= rh{
20-
r-=1;
21-
}
22-
}else{
23-
while l < height.len() && height[l] <= lh{
24-
l+=1;
25-
}
3+
let (mut max_area, mut l, mut r) = (0, 0, height.len() - 1);
4+
5+
while l < r {
6+
let area = ((r - l) as i32) * height[l].min(height[r]);
7+
max_area = area.max(max_area);
8+
9+
if height[l] > height[r] {
10+
r -= 1;
11+
} else {
12+
l += 1;
2613
}
2714
}
28-
29-
max
15+
16+
max_area
3017
}
31-
}
18+
}
19+
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
31+
32+

0 commit comments

Comments
 (0)