Solving tips
- Recognize the per-element question is really 'compare to an aggregate of the whole array' β here the array's maximum.
- Compute m = max(candies) once, then each kid's answer is simply candies[i] + extraCandies >= m; O(n) time, O(1) extra space.
- Pitfall: use >= not > since ties with the current max qualify (they only need to reach the greatest).
- Pitfall: don't recompute max inside the loop, which silently makes it O(n^2).
Problem
You are given an integer array candies where candies[i] is how many candies the i-th kid currently holds, plus an integer extraCandies. For each kid, decide: if you handed that kid all of the extraCandies, would they then have the greatest candy count among all kids? (Ties count β they only need to reach the maximum, not beat it.)
Return a list of booleans, one per kid, where the i-th entry is True exactly when giving all the extra candies to kid i makes their total greater than or equal to every other kidβs current count.
Examples
- Input:
candies = [2, 3, 5, 1, 3], extraCandies = 3 β Output: [True, True, True, False, True]
Kid 0 reaches 5, tying the current max of 5; kid 3 only reaches 4, which is short.
- Input:
candies = [4, 2, 1, 1, 2], extraCandies = 1 β Output: [True, False, False, False, False]
Only kid 0 (4 + 1 = 5) can reach or beat the max of 4; nobody else gets past 3.
- Input:
candies = [12, 1, 12], extraCandies = 10 β Output: [True, False, True]
Kid 1 reaches 11, still below the max of 12.
Constraints
2 <= n <= 100 where n = len(candies)
1 <= candies[i] <= 100
1 <= extraCandies <= 50
The bounds are tiny, but the intended solution is still the clean O(n) one.
Think about it first
Hint 1
For a fixed kid, what single number about the rest of the array do you actually need to answer "would they have the greatest count"?
Hint 2
Comparing kid i against every other kid repeats the same work n times. Can something be computed once, before the loop?
Hint 3
Compute `m = max(candies)` once. Kid i's answer is simply whether `candies[i] + extraCandies >= m` β note the current max always belongs to some kid, so comparing against it is safe even for that kid.
TL;DR
Precompute the max once, then one comparison per kid β O(n) time, O(1) extra space (beyond the output).
Approach 1 β Brute force
For each kid, boost their count and compare against every other kidβs current count.
from typing import List
class Solution:
def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]:
n = len(candies)
result = []
for i in range(n):
boosted = candies[i] + extraCandies
ok = True
for j in range(n):
if candies[j] > boosted:
ok = False
break
result.append(ok)
return result
Complexity: O(nΒ²) time, O(1) extra space.
With n β€ 100 this actually passes, but it redoes the identical βwhat is the biggest count?β scan n times β pure waste.
Approach 2 β Precompute the max
The insight: kid i beats-or-ties everyone if and only if candies[i] + extraCandies reaches the arrayβs current maximum. That maximum never changes across the loop, so compute it exactly once.
from typing import List
class Solution:
def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]:
top = max(candies)
return [c + extraCandies >= top for c in candies]
Walkthrough on candies = [2, 3, 5, 1, 3], extraCandies = 3:
top = max(candies) = 5.
- Kid 0: 2 + 3 = 5 β₯ 5 β
True.
- Kid 1: 3 + 3 = 6 β₯ 5 β
True.
- Kid 2: 5 + 3 = 8 β₯ 5 β
True.
- Kid 3: 1 + 3 = 4 < 5 β
False.
- Kid 4: 3 + 3 = 6 β₯ 5 β
True.
Result: [True, True, True, False, True] β matches the expected output.
Complexity: O(n) time (one pass for max, one for the comparisons), O(1) extra space beyond the returned list.
Common pitfalls
- Using strict
> instead of >= β the problem asks for the greatest count, and ties with the current maximum qualify.
- Worrying that kid i is compared against their own value inside
max(candies): itβs harmless, since candies[i] + extraCandies >= candies[i] always holds (extraCandies β₯ 1).
- Recomputing
max(candies) inside the loop β that silently turns the O(n) solution back into O(nΒ²).
Pattern takeaway
When a per-element question is really βhow does this element compare to some aggregate of the whole array?β, compute the aggregate (max, min, sum, a count table) in one pass first, then answer each element in O(1). Hoisting an invariant computation out of the loop is the simplest and most common array optimization.