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
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.248.143.163 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1749455414.A.2DD.html