LeetCode Solutions

575. Distribute Candies

Time: $O(n)$

Space: $O(200001)$

			

class Solution {
 public:
  int distributeCandies(vector<int>& candies) {
    bitset<200001> bitset;

    for (const int candy : candies)
      bitset.set(candy + 100000);

    return min(candies.size() / 2, bitset.count());
  }
};
			

class Solution {
  public int distributeCandies(int[] candies) {
    BitSet bitset = new BitSet(200001);

    for (final int candy : candies)
      bitset.set(candy + 100000);

    return Math.min(candies.length / 2, bitset.cardinality());
  }
}
			

class Solution:
  def distributeCandies(self, candies: List[int]) -> int:
    return min(len(candies) // 2, len(set(candies)))