楼主:
yam276 ('_')
2025-06-09 15:50:1211. Container With Most Water
题目:
给一个 Vec<i32> height
用两个成员当边做成水桶
计算最大的水桶容量
思路:
有以前写过的纪录 但效能不能再提升了
双指标就是速度最佳解 空间也差不多
写成链式也超丑 还是用旧版吧
Code:
impl Solution {
pub fn max_area(height: Vec<i32>) -> i32 {
let mut left = 0;
let mut right = height.len() - 1;
let mut max = 0;
while left < right {
let now = (right - left) as i32 * std::cmp::min(height[left],
height[right]);
if now > max {
max = now;
}
if height[left] < height[right] {
left += 1;
} else {
right -= 1;
}
}
max as i32
}
}