Isaac Emmanuel
15 Jun 2024•EasyC++ / Python / TypeScript

Distribute Candies

C++, Python and TypeScript solutions for Distribute Candies.

The Distribute Candies solutions are below.

Solution files

Python•distribute-candies/synced-solution.py
class Solution:
    def distributeCandies(self, candyType: List[int]) -> int:
        return min(len(set(candyType)), len(candyType) // 2)
C++•distribute-candies/synced-solution.cpp
class Solution {
public:
    int distributeCandies(vector<int>& candyType) {
        unordered_set<int> uniqueTypes(candyType.begin(), candyType.end());
        return min(static_cast<int>(uniqueTypes.size()), static_cast<int>(candyType.size() / 2));
    }
};
TypeScript•distribute-candies/synced-solution.ts
function distributeCandies(candyType: number[]): number {
    return Math.min(new Set(candyType).size, Math.floor(candyType.length / 2));
}