Alice has n candies, where the ith candy is of type candyType[i]. Alice noticed that she started to gain weight, so she visited a doctor.
The doctor advised Alice to only eat n / 2 of the candies she has (n is always even). Alice likes her candies very much, and she wants to eat the maximum number of different types of candies while still following the doctor's advice.
Given the integer array candyType of length n, return the maximum number of different types of candies she can eat if she only eats n / 2 of them.
Example 1:
Input: candyType = [1,1,2,2,3,3] Output: 3 Explanation: Alice can only eat 6 / 2 = 3 candies. Since there are only 3 types, she can eat one of each type.
Example 2:
Input: candyType = [1,1,2,3] Output: 2 Explanation: Alice can only eat 4 / 2 = 2 candies. Whether she eats types [1,2], [1,3], or [2,3], she still can only eat 2 different types.
Example 3:
Input: candyType = [6,6,6,6] Output: 1 Explanation: Alice can only eat 4 / 2 = 2 candies. Even though she can eat 2 candies, she only has 1 type.
Constraints:
n == candyType.length2 <= n <= 104n is even.-105 <= candyType[i] <= 105When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force strategy involves systematically creating every possible collection of candies the sister could receive. For each of these collections, we'll count how many unique types of candy she has and then find the highest count from all possibilities.
Here's how the algorithm would work step-by-step:
def distribute_candies_brute_force(candy_type_list):
import itertools
# First, determine the exact number of candies the sister can receive as per the rules.
sister_share_size = len(candy_type_list) // 2
max_unique_candy_types = 0
# Systematically generate every possible combination of candies the sister could choose.
all_possible_combinations = set(itertools.combinations(candy_type_list, sister_share_size))
# We must check each combination to find the one that maximizes the variety of candies.
for current_combination in all_possible_combinations:
unique_candies_in_this_combination = len(set(current_combination))
if unique_candies_in_this_combination > max_unique_candy_types:
max_unique_candy_types = unique_candies_in_this_combination
return max_unique_candy_typesThe core idea is to realize that the final answer is limited by one of two factors: either the number of candies Alice is allowed to eat, or the total number of unique candy types she possesses. The solution is simply to find both of these numbers and pick the smaller one.
Here's how the algorithm would work step-by-step:
def distribute_candies(candy_type_list):
# Using a set is the most direct way to find the count of all distinct candy types.
number_of_unique_candy_types = len(set(candy_type_list))
# The problem specifies that the person can only eat n/2 candies, creating a physical limit.
allowed_candies_to_eat = len(candy_type_list) // 2
# The answer is the lesser of the two constraints: available variety vs. eating capacity.
return min(number_of_unique_candy_types, allowed_candies_to_eat)| Case | How to Handle |
|---|---|
| Input array has the minimum possible length, n=2. | The solution correctly calculates Alice can eat 1 candy and returns the minimum of 1 and the number of unique types. |
| All candies in the input array are of the same type. | The algorithm correctly identifies only one unique type and returns 1, as this is the limiting factor. |
| All candies in the input array are of different types. | The algorithm correctly determines the limiting factor is the number of candies Alice can eat, n/2, and returns this value. |
| The number of unique candy types is exactly equal to n/2. | The solution correctly finds that the number of unique types and the number of allowed candies are equal, returning n/2. |
| Input array has the maximum possible length, n = 10^4. | A hash set-based solution with O(n) time and O(n) space complexity scales efficiently and avoids a timeout or memory error. |
| Input array contains negative numbers, zero, and values at the integer limits. | A standard hash set handles the full range of specified integer values correctly without any special logic. |
| The distribution of candy types is highly skewed, with some types appearing many times. | The solution's use of a set correctly counts each candy type only once, regardless of its frequency. |
| The input array is null, which is outside the problem's constraints. | A production-ready solution would handle this by returning 0, as no candies can be eaten from a non-existent list. |