575. Distribute Candies

原题地址: https://leetcode.com/problems/distribute-candies/description/
题目大意:给兄妹二人一些糖果(偶数个),种类不同,问妹妹能分到最多中糖果的种类数量。

记录糖果种类,若糖果种类大于数组的一半,妹妹最多得到candies/2种糖果,否则每种糖果都可以得到(简直就是小学奥数题,太无聊了。。)

def distributeCandies(self, candies):
        """
        :type candies: List[int]
        :rtype: int
        """
        len1 = len(set(candies))
        if len1 > len(candies)/2:
            return int(len(candies)/2)
        else:
            return len1

    def distributeCandies2(self, candies):
        """
        :type candies: List[int]
        :rtype: int
        """
        sister_count = len(candies) // 2
        unique_candies = set(candies)
        return min(sister_count, len(unique_candies))

方法一写的比较直白,方法二通过比较的方法。

知识点

一开始发现/返回的是浮点型有一位小数,就用了int()方法,其实只要用地板除//就行了,返回的是最近接的整数没有小数点




所有题目解题方法和答案代码地址:https://github.com/fredfeng0326/LeetCode
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容