思路:为了获取最大的种类,同时,不需要满足平均分配和相同,故只需要统计种类的个数和分配数量个数大小即可;
int distributeCandies(vector<int>& candyType) {
if (candyType.size() == 2) {
return 1;
}
set<int> s;
for (int i = 0; i < candyType.size(); i++) {
s.insert(candyType[i]);
}
if (s.size() >= candyType.size() / 2) {
return candyType.size() / 2;
}
return s.size();
}
思路:模拟
vector<int> distributeCandies(int candies, int num_people) {
vector<int> res(num_people, 0);
int i = 0;
int temp = 1;
while (candies != 0) {
int index = i % num_people;
if (candies >= temp) {
res[index] += temp;
candies -= temp;
} else {
res[index] += candies;
candies = 0;
}
temp += 1;
i += 1;
}
return res;
}