Re: [閒聊] 每日leetcode
※ 引述《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
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.248.143.163 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1710734188.A.EA8.html
※ 編輯: yam276 (60.248.143.163 臺灣), 03/18/2024 11:57:49
推
03/18 11:58,
1年前
, 1F
03/18 11:58, 1F
→
03/18 11:59,
1年前
, 2F
03/18 11:59, 2F
討論串 (同標題文章)