575. Distribute Candies

时间:2022-04-24 06:17:14

https://leetcode.com/problems/distribute-candies/description/

题目比较长,总结起来很简单:有个整型数组,长度是偶数,把它分成两份,要求有一份里面数字的多样性最大。

思路:一个很简单的思路是,数出有多少不一样的数字n,然后一份有多少个m,取两个的最小值就行。

class Solution {
public:
int distributeCandies(vector<int>& candies) {
if (candies.size() == ) return ;
std::sort(candies.begin(), candies.end());
int distinct = ; for (int i = ; i < candies.size(); i++) {
if (candies[i-] != candies[i]) {
distinct++;
}
} //how many candies for each one, since amount of candies is even and only split to 2 parts
int n = candies.size() / ;
return std::min(n, distinct);
}
};