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:
Example 2:
Note:
The length of the given array is in range [2, 10,000], and will be even.
The number in given array is in range [-100,000, 100,000].
解法1:
有一类问题的思路是这样的:如果求最大或者最小值,先考虑一下理论上可能可以取到的值。然后再以此为突破口看看是否有思路。
这题就是这样,先想到要取n/2个数,那么最大的种类也就是n/2,不可能再大了。那么如果数组里的类别比n/2小,那么最大的值就是数组里的数的种类数。
C++
Java