leetcode每日一题-575:分糖果

leetcode每日一题-575:分糖果,第1张

leetcode每日一题-575:分糖果 leetcode每日一题-575:分糖果 链接 分糖果

题目



分析

我们先统统计所有糖果的种类,如果糖果的种类小于等于n/2,那么她就可以吃到所有种类的糖,如果糖的种类大于n/2,那么她就只能吃到n/2种类型的糖.



代码

C++

class Solution {
public:
    int distributeCandies(vector& candyType) {
        int n = candyType.size();
        unordered_map m;
        for(int x : candyType) m[x]++;
        return min((int)m.size(), n / 2);
    }
};

java

class Solution {
    public int distributeCandies(int[] candyType) {
        Set set = new HashSet();
        for (int candy : candyType) {
            set.add(candy);
        }
        return Math.min(set.size(), candyType.length / 2);
    }
}

作者:LeetCode-Solution

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/4993382.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-14
下一篇2022-11-14

发表评论

登录后才能评论

评论列表(0条)

    保存