# [LeetCode] Distribute Candies 分糖果

Given an integer array with even length, where different numbers in this array represent different kinds of candies. Each number means one candy of the corresponding kind. You need to distribute these candies equally in number to brother and sister. Return the maximum number of kinds of candies the sister could gain.

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. 

Note:

1. The length of the given array is in range [2, 10,000], and will be even.
2. The number in given array is in range [-100,000, 100,000].

public:
int distributeCandies(vector<int>& candies) {
unordered_set<int> s;
for (int candy : candies) s.insert(candy);
return min(s.size(), candies.size() / 2);
}
};

public:
int distributeCandies(vector<int>& candies) {
return min(unordered_set<int>(candies.begin(), candies.end()).size(), candies.size() / 2);
}
};

https://discuss.leetcode.com/topic/88582/c-1-liner

https://discuss.leetcode.com/topic/88657/java-8-one-line-solution-o-n

https://discuss.leetcode.com/topic/88507/java-solution-3-lines-hashset

，如需转载请自行联系原博主。

|
3月前
|

27 0
|
10月前
【Leetcode -575.分糖果 -594.最长和谐子序列】
【Leetcode -575.分糖果 -594.最长和谐子序列】
43 0
|
2月前
|

30 1
|
2月前
|

19 0
|
2月前
LeetCode575——分糖果
LeetCode575——分糖果
14 0
|
2月前
|

【经典算法】LeetCode 1103 分糖果 II(Java/C/Python3实现含注释说明,Easy)
【经典算法】LeetCode 1103 分糖果 II(Java/C/Python3实现含注释说明,Easy)
36 0
|
3月前
|

[经典力扣面试题]135. 分发糖果
[经典力扣面试题]135. 分发糖果
24 1
|
3月前
|

☆打卡算法☆LeetCode 135. 分发糖果 算法解析
☆打卡算法☆LeetCode 135. 分发糖果 算法解析
50 0
|
3月前
LeetCode 20200601 打卡　1431. 拥有最多糖果的孩子
LeetCode 20200601 打卡　1431. 拥有最多糖果的孩子
16 0
|
9月前
|

44 0