https://leetcode.com/problems/distribute-candies/description/
题目比较长,总结起来很简单:有个整型数组,长度是偶数,把它分成两份,要求有一份里面数字的多样性最大。
思路:一个很简单的思路是,数出有多少不一样的数字n,然后一份有多少个m,取两个的最小值就行。
class Solution { public: int distributeCandies(vector<int>& candies) { if (candies.size() == 0) return 0; std::sort(candies.begin(), candies.end()); int distinct = 1; for (int i = 1; i < candies.size(); i++) { if (candies[i-1] != 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() / 2; return std::min(n, distinct); } };