3 Line Java with Explanation


  • 0
    K

    If more than half of the candies are unique, the sister can get at most size / 2 kinds of candies since it's evenly distributed by candy count. If less than half of the candies are unique, the sister can get at most the number of unique candies.

    public int distributeCandies(int[] candies) {
        Set<Integer> set = new HashSet<Integer>();
        for (int num : candies) set.add(num);
        return Math.min(candies.length/2, set.size());
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.