Kids With the Greatest Number of Candies
Kids With the Greatest Number of Candies is LeetCode 1431 (Easy). candies[i] is how many candies kid i has, and you hold extraCandies more. For each kid, imagine giving them all the extra candies. Return a list of booleans: true if that kid would then have at least as many candies as every other kid.
- Each kid is a separate "what if". The extras are never split, and the other kids keep exactly what they had.
- A tie for the most counts as having the greatest number.
- 2 <= n <= 100 (n is the number of kids)
- 1 <= candies[i] <= 100
- 1 <= extraCandies <= 50
Intuition
When kid i gets the extras, nobody else changes. So kid i ends up with the most exactly when candies[i] + extraCandies is at least the largest count among the other kids.
Compare every kid against the current maximum of the whole array. Using the whole array instead of "everyone except kid i" is safe: if kid i is the one holding the maximum, they already reach it before any extras. Find the maximum in one pass, then answer every kid with one addition and one comparison.
Every element is checked against the same fixed reference value, here the largest count. Compute the reference once before the loop instead of rebuilding it for each element.
Approach
Before reading on, take candies = [3, 7, 2] and extraCandies = 4. Decide what kid 0 should get, and whether your comparison uses > or >=.
Find the most candies any kid has
Scan the array once and keep the largest value. This is the bar every kid has to reach, and it does not move, because giving extras to one kid is only imagined and never changes the array.
Give each kid the extras and compare
For kid i, test candies[i] + extraCandies >= most. Use >=, because the problem treats a kid who ties for the most as having the greatest number.
Collect the answers in order
Append one boolean per kid, in the same order as candies, so position i of the result answers the question for kid i.
Code in Python, C++ and Java
>=. The other kids keep their own counts.>=. The other kids keep their own counts.Common pitfalls
Using > so that ties fail
count + extraCandies > most
count + extraCandies >= most
A kid who draws level with the leader does have the greatest number. With candies = [2, 3, 5, 1, 3] and extraCandies = 3, kid 0 reaches 2 + 3 = 5, the same as the leader, so the answer starts with true; with > it starts with false.
Edge cases
Each kid already equals the maximum, so every answer is true no matter how many extras there are.
Complexity
Kids With the Greatest Number of Candies FAQ
Can the answer be built in a single pass?
Not left to right without going back. Kid 0's answer depends on kids that come later, since one of them might hold the maximum. You need the maximum first, so two passes over the array is the natural minimum.
Do the extra candies get shared between kids?
No. Each kid is asked about separately, as if all of extraCandies went to that one kid and nobody else got anything. That is why the bar stays fixed at the original maximum for every kid.