题目描述:给定一个偶数长度的数组,其中不同的数字代表着不同种类的糖果,每一个数字代表一个糖果。你需要把这些糖果平均分给一个弟弟和一个妹妹。返回妹妹可以获得的最大糖果的种类数。(难度:简单)
原题链接:
575. 分糖果 - 力扣(LeetCode) (leetcode-cn.com)
C++代码:
class Solution {
public:
int distributeCandies(vector<int>& candyType) {
sort(candyType.begin(),candyType.end());
int last=0x3f3f3f3f,res=0;
for(auto &c:candyType){
if(c!=last) res++,last=c;
}
return min(res,(int)candyType.size()/2);
}
};