[閒聊] 每日leetcode 75 - Day2 - 2

作者: yam276 ('_')   2025-05-28 18:43:57
1431. Kids With the Greatest Number of Candies
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/
都簡單題就多寫一點
題意:
每個小孩有不同數量的糖果
你有一疊 e 顆糖果
要是每次把 e 顆糖果給一個小孩
能不能比原本擁有最多糖果的小孩多
Code:
impl Solution {
pub fn kids_with_candies(candies: Vec<i32>, extra_candies: i32)
-> Vec<bool>
{
let max_candies = candies.iter().max().unwrap();
candies
.iter()
.map(|&x| x + extra_candies >= *max_candies)
.collect()
}
}
一行鏈式解:
candies.iter()
.map(|&x| x + extra_candies >= *candies.iter().max().unwrap())
.collect()

Links booklink

Contact Us: admin [ a t ] ucptt.com