Container With Most Water in Rust

impl Solution {
    fn max_area(height: Vec<i32>) -> i32 {
        // Initialize left and right pointers
        let (mut left, mut right) = (0, height.len() - 1);
        // Initialize max_area to store the maximum area found
        let mut max_area = 0;

        // Continue looping until the left pointer is not the same as the right pointer
        while left < right {
            // Calculate the width between the two lines
            let width = (right - left) as i32;
            // Find the height of the shorter line between the two
            let min_height = std::cmp::min(height[left], height[right]);
            // Calculate the area with the current configuration
            let current_area = width * min_height;
            // Update max_area if the found area is larger
            max_area = std::cmp::max(max_area, current_area);

            // Move the pointer of the shorter line inward
            if height[left] < height[right] {
                left += 1;
            } else {
                right -= 1;
            }
        }

        // Return the maximum area found
        max_area
    }
}

PrevNext