Distribute Candies (LeetCode 575): Solution and Explanation
Alice may eat half of her n candies and wants as many different types as possible. Return the largest number of types she can eat.
Intuition
Two separate limits cap the answer. She eats only n / 2 candies, so she cannot taste more than n / 2 types. She also cannot eat a type that does not exist, so she cannot taste more than the number of distinct types.
Both limits can be reached at the same time: pick one candy of each type until the plate is full, and if types run out first, fill the rest with repeats. So the answer is exactly min(distinct types, n / 2).
A set collects the distinct types. Once it holds n / 2 types the answer is fixed, so the scan can stop early.
Approach: Set of types, capped at n / 2
- Set
limit = n / 2. - Walk the candies and add each type to a set.
- If the set size reaches
limit, returnlimitat once. - If the loop ends first, return the set size.
Step-by-step example
candyType = [1,1,2,3]
- n = 4, so limit = 2.
- Candy 1 adds type 1. The set has 1 type.
- Candy 1 again adds nothing new.
- Candy 2 adds type 2. The set has 2 types, which equals limit, so return 2.
Code: Python, Java, C++
1class Solution:
2 def distributeCandies(self, candyType: List[int]) -> int:
3 limit = len(candyType) // 2
4 kinds = set()
5 for candy in candyType:
6 kinds.add(candy)
7 if len(kinds) == limit:
8 return limit
9 return len(kinds)1class Solution {
2 public int distributeCandies(int[] candyType) {
3 int limit = candyType.length / 2;
4 Set<Integer> kinds = new HashSet<>();
5 for (int candy : candyType) {
6 kinds.add(candy);
7 if (kinds.size() == limit) return limit;
8 }
9 return kinds.size();
10 }
11}1class Solution {
2public:
3 int distributeCandies(vector<int>& candyType) {
4 int limit = candyType.size() / 2;
5 unordered_set<int> kinds;
6 for (int candy : candyType) {
7 kinds.insert(candy);
8 if ((int)kinds.size() == limit) return limit;
9 }
10 return kinds.size();
11 }
12};Time and space complexity
O(n)
Each candy is added to the set once, at O(1) average cost.
O(n)
The set holds at most min(n / 2, distinct types) values because the scan stops at the cap.
Other approaches
One-liner (time O(n), space O(n))
Return min(len(set(candyType)), len(candyType) // 2). The same idea without the early stop.
Sort and count changes (time O(n log n), space O(1) besides the sort)
Sort the types and count how many times the value changes between neighbours, then cap at n / 2. Avoids a hash set.
Common mistakes
- Returning the number of distinct types without the n / 2 cap. With [1,2,3,4] she may eat only 2 candies.
- Returning n / 2 without checking how many types exist. With [6,6,6,6] only one type exists.
- Using a boolean array indexed by type without offsetting negatives. Types go down to -10^5.
Frequently asked questions
Why is min(types, n / 2) always reachable?
If there are at least n / 2 types, she eats one candy from n / 2 different types. If there are fewer, she eats one of each type and fills the remaining slots with repeats, which do not lower the count.
Does the early return matter?
It does not change the answer or the worst case, but it stops the scan as soon as the plate is full of distinct types, which saves work when there are many types.