其实就是问你数组里不同的元素能否超过二分之一。
不上描述了,上example吧
Example 1:
Input: candies = [1,1,2,2,3,3]
Output: 3
Explanation:
There are three different kinds of candies (1, 2 and 3), and two candies for each kind.
Optimal distribution: The sister has candies [1,2,3] and the brother has candies [1,2,3], too.
The sister has three different kinds of candies.
Example 2:
Input: candies = [1,1,2,3]
Output: 2
Explanation: For example, the sister has candies [2,3] and the brother has candies [1,1].
The sister has two different kinds of candies, the brother has only one kind of candies.
class Solution {
public int distributeCandies(int[] candies) {
HashSet<Integer> set = new HashSet<Integer>();
for(int i = 0;i<candies.length;i++)
{
set.add(candies[i]);
}
if(set.size()>=candies.length/2)
return candies.length/2;
return set.size();
}
}