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.

妹妹最多拿到多少种糖果。可以让妹妹尽可能拿不同种类的糖果,限制条件是每人一半,如果糖果种类超过了总数的一般,那么妹妹就可以拿到总数一般种类的糖果,如果不足糖果总数的一半,那么可以拿到所有种类糖果。

class Solution {
    public int distributeCandies(int[] candies) {
        Set<Integer> set = new HashSet<Integer>();
        for(int i = 0; i < candies.length; i++) {
            set.add(candies[i]);
        }

        return Math.min(set.size(), candies.length / 2);
    }
}

results matching ""

    No results matching ""