zoukankan      html  css  js  c++  java
  • 575. Distribute Candies

    You have n candies, the ith candy is of type candies[i].

    You want to distribute the candies equally between a sister and a brother so that each of them gets n / 2 candies (n is even). The sister loves to collect different types of candies, so you want to give her the maximum number of different types of candies.

    Return the maximum number of different types of candies you can give to the sister.

    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.
    

    Example 3:

    Input: candies = [1,1]
    Output: 1
    

    Example 4:

    Input: candies = [1,11]
    Output: 1
    

    Example 5:

    Input: candies = [2,2]
    Output: 1
    

    Constraints:

    • n == candies.length
    • 2 <= n <= 10^4
    • n is even.
    • -10^5 <= candies[i] <= 10^5

    hint:
    To maximize the number of kinds of candies,
    we should try to distribute candies such that sister will gain all kinds.
    Inserting all candies kind in the hashset and then checking its size with upper limit.

    time = O(n), space = O(n)

    class Solution {
        public int distributeCandies(int[] candies) {
            int n = candies.length;
            Set<Integer> set = new HashSet<>();
            for(int candy : candies) {
                set.add(candy);
            }
            return set.size() < n / 2 ? set.size() : n / 2;
        }
    }
  • 相关阅读:
    C字符串和C++字符串
    Linux的用户态和内核态
    知乎问答:如何理解“In UNIX, everything is a file”?
    科普:并行计算、分布式计算、集群计算和云计算
    个人使用VIM的一些总结
    C语言回调函数学习
    Python的函数参数传递:传值?引用?
    ECMAScript 函数函数概述
    ECMAScript 函数arguments 对象
    ECMAScript 语句with 语句
  • 原文地址:https://www.cnblogs.com/fatttcat/p/13534610.html
Copyright © 2011-2022 走看看