Sponsored
Sponsored
The goal is to find the maximum number of unique types of candies Alice can eat. We can take the following steps:
maxCandies = n / 2
.maxCandies
.Time Complexity: O(n) because we iterate over the array to create the set.
Space Complexity: O(n) for storing the unique types in a set.
1def distributeCandies(candyType):
2 return min(len(set(candyType)), len(candyType) // 2)
This solution creates a set from the list of candies to determine how many unique types there are. The minimum of the number of unique types and n / 2
candies Alice can eat is the answer.
For another perspective:
n / 2
.Time Complexity: O(n) for creating the counter.
Space Complexity: O(n) for storing unique types in the counter.
1
This JavaScript solution leverages plain objects as maps for counting candy occurrences, returning the minimal constraint of unique types and n / 2
. Object.keys gives the distinct types.