575. Distribute Candies
EasySolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
Alice has n candies and is advised to eat only n / 2 of them. The goal is to maximize the number of different types of candies she eats. This problem asks for the maximum number of unique candy types Alice can have in her selection of n / 2 candies.
Solution
Distribute Candies in Python
class Solution:
def distributeCandies(self, candyType: List[int]) -> int:
# The number of candies Alice can eat.
n = len(candyType)
can_eat = n // 2
# Find the number of unique candy types using a set.
# The set() constructor can take an iterable to create a set of unique elements.
num_unique_types = len(set(candyType))
# The maximum number of different types Alice can eat is the minimum of
# the number of candies she can eat and the number of unique types available.
return min(can_eat, num_unique_types)
Distribute Candies in Java
class Solution {
public int distributeCandies(int[] candyType) {
// The number of candies Alice can eat.
int canEat = candyType.length / 2;
// Use a HashSet to find the number of unique candy types.
Set<Integer> uniqueTypes = new HashSet<>();
for (int type : candyType) {
uniqueTypes.add(type);
}
// The number of unique types of candies.
int numUniqueTypes = uniqueTypes.size();
// The maximum number of different types Alice can eat is the minimum of
// the number of candies she can eat and the number of unique types available.
return Math.min(canEat, numUniqueTypes);
}
}
Distribute Candies in C++
class Solution {
public:
int distributeCandies(vector<int>& candyType) {
// Create an unordered_set directly from the vector to count unique types.
std::unordered_set<int> uniqueTypes(candyType.begin(), candyType.end());
// The maximum number of different types of candies Alice can eat is the minimum of:
// 1. The number of candies she is allowed to eat (n / 2).
// 2. The total number of unique candy types available.
return std::min((int)candyType.size() / 2, (int)uniqueTypes.size());
}
};
Distribute Candies in TypeScript
function distributeCandies(candyType: number[]): number {
// The number of candies Alice can eat.
const canEat = candyType.length / 2;
// Use a Set to find the number of unique candy types.
// The Set constructor can take an iterable to create a set of unique elements.
const uniqueTypes = new Set(candyType);
// The number of unique types of candies.
const numUniqueTypes = uniqueTypes.size;
// The maximum number of different types Alice can eat is the minimum of
// the number of candies she can eat and the number of unique types available.
return Math.min(canEat, numUniqueTypes);
};
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.