题目

类型:Array
image.png

解题思路

贪心算法
image.png

代码

  1. class Solution {
  2. public int distributeCandies(int[] candyType) {
  3. Set<Integer> set = new HashSet<Integer>();
  4. for (int candy : candyType) {
  5. set.add(candy);
  6. }
  7. return Math.min(set.size(), candyType.length / 2);
  8. }
  9. }