pub fn max_area(height: Vec<i32>) -> i32 {
alg_1(height)
}
use core::cmp::max;
fn alg_1(height: Vec<i32>) -> i32 {
let mut sidx: usize = 0;
let mut eidx = height.len() - 1;
let mut area_max = 0;
let area = |x: usize, y: usize| -> i32 {
return if height[x] >= height[y] {
height[y]
} else {
height[x]
} * (if x >= y { x - y } else { y - x }) as i32;
};
loop {
if sidx == eidx {
break;
}
area_max = max(area(sidx, eidx), area_max);
let mut moved = false;
if height[sidx] <= height[eidx] {
while sidx < eidx {
if height[sidx + 1] <= height[sidx] {
sidx += 1;
moved = true;
} else {
break;
}
}
if !moved {
sidx += 1;
}
} else {
while sidx < eidx {
if height[eidx - 1] <= height[eidx] {
eidx -= 1;
moved = true;
} else {
break;
}
}
if !moved {
eidx -= 1;
}
}
}
area_max
}