Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1304 from zim0369/container-with-water
Browse files Browse the repository at this point in the history
Update 11-Container-With-Most-Water.rs
  • Loading branch information
Ahmad-A0 authored Oct 21, 2022
2 parents ad0c70e + 2d5c8eb commit 0332f3b
Showing 1 changed file with 13 additions and 26 deletions.
39 changes: 13 additions & 26 deletions rust/11-Container-With-Most-Water.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,18 @@
impl Solution {
pub fn max_area(height: Vec<i32>) -> i32 {
let (mut l, mut r) = (0, height.len() - 1);

let mut max = 0;

while (l < r){
let (lh, rh) = (height[l], height[r]);
let h = lh.min(rh);

let d = (r - l) as i32;
let area = d * h;

if area > max{
max = area;
}

if rh < lh{
while r > 0 && height[r] <= rh{
r-=1;
}
}else{
while l < height.len() && height[l] <= lh{
l+=1;
}
let (mut max_area, mut l, mut r) = (0, 0, height.len() - 1);

while l < r {
let area = ((r - l) as i32) * height[l].min(height[r]);
max_area = area.max(max_area);

if height[l] > height[r] {
r -= 1;
} else {
l += 1;
}
}
max

max_area
}
}
}

0 comments on commit 0332f3b

Please sign in to comment.