1431. Child with the most candies (C++)
1 topic description
Give you an array candies and an integer extraCandies, where candies[i] represents the number of candies owned by the i-th child.
For each child, check whether there is a plan. After allocating extra Candies to the children, this child has the most candies. Note that multiple children are allowed to have the maximum number of candies at the same time.
2 Example description
2.1 Example 1
Input: candies = [2,3,5,1,3], extraCandies = 3
Output: [true,true,true,false,true]
Explanation:
Child 1 has 2 candies, if he gets all the extra candies (3 ), then he has 5 candies in total, and he will become the child with the most candies.
Child 2 has 3 candies. If he gets at least 2 extra candies, then he will be the child with the most candies.
Child 3 has 5 candies, and he is already the child with the most candies.
Child 4 has 1 candy. Even if he gets all the extra candies, he only has 4 candies and cannot be the child with the most candies.
Child 5 has 3 candies. If he gets at least 2 extra candies, then he will be the child with the most candies.
2.2 Example 2
Input: candies = [4,2,1,1,2], extraCandies = 1
Output: [true,false,false,false,false]
Explanation: There is only 1 extra candies, so no matter who the extra candies are given, only child 1 Can be the child with the most candies.
2.3 Example 3
Input: candies = [12,1,12], extraCandies = 10
Output: [true,false,true]
3 Problem solving tips
2 <= candies.length <= 100
1 <= candies[i] <= 100
1 <= extraCandies <= 50
4 Detailed source code (C++)
class Solution {
public:
vector<bool> kidsWithCandies(vector<int>& candies, int extraCandies) {
int max = 0 ;
vector<bool> res;
for (int i = 0 ; i < candies.size() ; i++)
{
if (candies[i] > max)
{
max = candies[i];
}
}
for (int i = 0 ; i < candies.size() ; i++)
{
if (max <= candies[i] + extraCandies)
{
res.push_back(true);
}
else
{
res.push_back(false);
}
}
return res;
}
};