Skip to content

Commit 0332f3b

Browse files
authored
Merge pull request neetcode-gh#1304 from zim0369/container-with-water
Update 11-Container-With-Most-Water.rs
2 parents ad0c70e + 2d5c8eb commit 0332f3b

File tree

1 file changed

+13
-26
lines changed

1 file changed

+13
-26
lines changed
Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,18 @@
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+
}

0 commit comments

Comments
 (0)