DEV Community

Discussion on: Container with Most Water

Collapse
 
jaseemabid profile image
Jaseem Abid

I came up with something slightly different:

pub fn max_area(height: Vec<i32>) -> i32 {
    let mut best = 0;

    for left in 0..height.len() {
        for right in (left + 1)..height.len() {
            let width = (right - left) as i32;
            let height = height[left].min(height[right]);
            let volume = width * height;

            best = best.max(volume);
        }
    }

    best
}
Collapse
 
steadbytes profile image
Ben Steadman

This is correct, but have you considered the runtime complexity? What happens if a large input, say 10000 elements is provided?

Take a look at this playground for an example (it is likely to timeout so you may wish to copy the code and run it locally) :)