[閒聊] 每日leetcode 75 - Day7 - 3

作者: yam276 ('_')   2025-06-09 15:50:12
11. 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
}
}

Links booklink

Contact Us: admin [ a t ] ucptt.com