Re: [閒聊] 每日leetcode

作者: yam276 ('_')   2024-03-18 11:56:26
※ 引述《oin1104 (是oin的說)》之銘言:
: 題目:
: 給你一串氣球陣列
: 用垂直x軸的箭射破他們
: 擦到邊就可以了
先用右邊界整理
然後開始射飛鏢
在邊界外代表要多射一支
Code:
impl Solution {
pub fn find_min_arrow_shots(mut points: Vec<Vec<i32>>) -> i32 {
points.sort_unstable_by_key(|k| k[1]);
let mut result = 1;
let mut arrow_pos = points[0][1];
for i in 1..points.len() {
if points[i][0] > arrow_pos {
result += 1;
arrow_pos = points[i][1];
}
}
result
}
}
別人跟鬼一樣的的FP魔術詠唱:
impl Solution {
pub fn find_min_arrow_shots(mut points: Vec<Vec<i32>>) -> i32 {
points.sort_unstable_by_key(|b| b[1]);
points.iter().skip(1).fold((1, points[0][1]), |(rez, right), b|
if right < b[0] {
(rez + 1, b[1])
} else {
(rez, right)
}
).0
}
}
作者: oin1104 (是oin的說)   2024-03-18 11:58:00
大師
作者: yam276 ('_')   2024-03-18 11:59:00
sort_unstable_by_key效率屌虐sort_by_key 沒次要要求就用

Links booklink

Contact Us: admin [ a t ] ucptt.com