Distribute Candies - Problem
Candy Distribution Problem
Alice has collected
Alice wants to maximize her candy-eating experience by consuming the maximum number of different candy types while following her doctor's advice. Given an integer array
Example: If Alice has 6 candies of types
Alice has collected
n candies of various types, where each candy is represented by candyType[i]. However, her doctor has advised her to eat only half of her candies (n/2) to maintain her health.Alice wants to maximize her candy-eating experience by consuming the maximum number of different candy types while following her doctor's advice. Given an integer array
candyType of length n (where n is always even), return the maximum number of different types of candies Alice can eat if she only eats n/2 of them.Example: If Alice has 6 candies of types
[1,1,2,2,3,3], she can eat 3 candies. To maximize variety, she should pick one candy of each type (1, 2, and 3), giving her 3 different types. Input & Output
example_1.py โ Basic Case
$
Input:
[1,1,2,2,3,3]
โบ
Output:
3
๐ก Note:
Alice has 6 candies with 3 different types. She can eat 6/2 = 3 candies. Since there are exactly 3 unique types, she can eat one of each type to maximize variety.
example_2.py โ Limited by Candy Count
$
Input:
[1,1,2,3]
โบ
Output:
2
๐ก Note:
Alice has 4 candies with 3 different types. She can only eat 4/2 = 2 candies. Even though there are 3 unique types, she's limited to eating only 2 different types.
example_3.py โ All Same Type
$
Input:
[6,6,6,6]
โบ
Output:
1
๐ก Note:
Alice has 4 candies but they're all the same type. She can eat 4/2 = 2 candies, but since there's only 1 unique type, she can only experience 1 different candy type.
Constraints
- n == candyType.length
- 2 โค n โค 104
- n is even
- -105 โค candyType[i] โค 105
Visualization
Tap to expand
Understanding the Visualization
1
Survey the Options
Count how many different types of candies are available
2
Check Your Limit
Determine how many candies you're allowed to eat (n/2)
3
Maximize Variety
Take the minimum of available types and your eating limit
Key Takeaway
๐ฏ Key Insight: The answer is always min(unique_candy_types, n/2). Alice maximizes variety by choosing one candy from each unique type, limited by either available variety or eating constraint.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code