g0001_0100.s0011_container_with_most_water.Solution.rs Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-all Show documentation
Show all versions of leetcode-in-all Show documentation
104 LeetCode algorithm problem solutions
The newest version!
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Greedy #Two_Pointers
// #Algorithm_II_Day_4_Two_Pointers #Big_O_Time_O(n)_Space_O(1)
// #2024_09_04_Time_4_ms_(95.02%)_Space_2.9_MB_(95.02%)
impl Solution {
pub fn max_area(height: Vec) -> i32 {
let mut max_area = 0;
let mut left = 0;
let mut right = height.len() as i32 - 1;
while left < right {
let h_left = height[left as usize];
let h_right = height[right as usize];
if h_left < h_right {
max_area = max_area.max(h_left * (right - left));
left += 1;
} else {
max_area = max_area.max(h_right * (right - left));
right -= 1;
}
}
max_area
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy