575. Distribute Candies

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.
示例:

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. 
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. 

就是看数组里面的不重复元素有多少个,然后和总数的一半对比就行,
唯一是set()的用法,注意

python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联合), intersection(交), difference(差)和sysmmetric difference(对称差集)等数学运算.

sets 支持 x in set, len(set),和 for x in set。作为一个无序的集合,sets不记录元素位置或者插入点。因此,sets不支持 indexing, slicing, 或其它类序列(sequence-like)的操作。

代码:

    def distributeCandies(self, candies):    
        """
        :type candies: List[int]
        :rtype: int
        """
        return min(len(candies)/2,len(set(candies)))
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,493评论 0 23
  • 刚刚看了一个视频,是一个小男孩,大概7/8岁的样子。在大街上找人要2块钱,要做公交车。 摄像机跟拍了大概8/9个人...
    淡雾先生阅读 3,685评论 0 1
  • 塞翁失马,焉知非福。塞翁得马,焉知非祸。 几天前,我在县城一家早餐店门前,发现一口器型适中、图纹优美的小水缸,正是...
    老盼2125阅读 2,682评论 3 2
  • 当你的脸上对我充满笑容时 我却忽视了对你的关怀和呵护 当你脸上不再拥有对我的鼓励和笑容时 我感到了无限的痛苦和无助...
    流年时光_fe16阅读 2,224评论 0 2

友情链接更多精彩内容