Skip to content

575. Distribute Candies

  • Time: $O(n)$
  • Space: $O(200001)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
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.contains());
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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());
  }
}
1
2
3
class Solution:
  def distributeCandies(self, candies: list[int]) -> int:
    return min(len(candies) // 2, len(set(candies)))