Skip to content

Commit 4b77d54

Browse files
authored
Merge pull request #2514 from DaleStudy/week6
container-with-most-water
2 parents 6e0c806 + b1f9499 commit 4b77d54

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// TC: O(n)
2+
// SC: O(1)
3+
impl Solution {
4+
pub fn max_area(height: Vec<i32>) -> i32 {
5+
let mut max_area = 0;
6+
let (mut left, mut right) = (0, height.len() - 1);
7+
while left < right {
8+
let width = (right - left) as i32;
9+
let min_height = height[left].min(height[right]);
10+
let area = width * min_height;
11+
max_area = max_area.max(area);
12+
if height[left] < height[right] {
13+
left += 1;
14+
} else {
15+
right -= 1;
16+
}
17+
}
18+
max_area
19+
}
20+
}

0 commit comments

Comments
 (0)