website/content/ChapterFour/0500~0599/0575.Distribute-Candies.md
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:
给定一个偶数长度的数组,其中不同的数字代表着不同种类的糖果,每一个数字代表一个糖果。你需要把这些糖果平均分给一个弟弟和一个妹妹。返回妹妹可以获得的最大糖果的种类数。
n/2 小,那么就返回 len(map),否则返回 n/2 (即一半都分给姐妹)。
package leetcode
func distributeCandies(candies []int) int {
n, m := len(candies), make(map[int]struct{}, len(candies))
for _, candy := range candies {
m[candy] = struct{}{}
}
res := len(m)
if n/2 < res {
return n / 2
}
return res
}