[LeetCode] Kids With the Greatest Number of Candies

1431. Kids With the Greatest Number of Candies

There are n kids with candies. You are given an integer array candies, where each candies[i] represents the number of candies the ith kid has, and an integer extraCandies, denoting the number of extra candies that you have.

Return a boolean array result of length n, where result[i] is true if, after giving the ith kid all the extraCandies, they will have the greatest number of candies among all the kids**, or false otherwise.

Note that multiple kids can have the greatest number of candies.

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
vector<bool> kidsWithCandies(vector<int>& candies, int extraCandies) {
int ma = *max_element(begin(candies), end(candies));
vector<bool> res;
for(auto c : candies) {
res.push_back(c + extraCandies >= ma);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/04/17/PS/LeetCode/kids-with-the-greatest-number-of-candies/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.