575. Distribute Candies

来源:互联网 发布:dbc2000 传奇数据库表 编辑:程序博客网 时间:2024/06/10 23:34

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.Example 1:Input: candies = [1,1,2,2,3,3]Output: 3Explanation: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: 2Explanation: 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. 

解:看由题可以看出是求数组中不同字符的个数,可以通过python中集合(set)求解,即集合的长度

    def distributeCandies(candies):        """        :type candies: List[int]        :rtype: int        """        size = len(set(candies))        num = len(candies) / 2        if size > num:            size = num        return size
0 0
原创粉丝点击