1163. Candies

1163. Candies

Chinese English

Given an even-numbered integer array, different numbers in the array represent different kinds of candy, and each number represents a kind of candy. You need to distribute these sweets evenly to your brother and sister. Return the maximum number of candy types that your sister can get.

Sample

输入: candies = [1,1,2,2,3,3]
输出: 3
解释:
有三种不同的糖果(1, 2 and 3), 每种糖果有两个。
最佳分法:妹妹拥有[1,2,3] ,弟弟也会拥有拥有[1,2,3]。
妹妹拥有3种不同的糖果。

Precautions

The length of the given array is [2, 10,000], and it is even.
The range of numbers in the given array is [-100,000, 100,000].

Enter test data (one parameter per line) How to understand test data?
## 分 Candy
 class Solution:
     '' '
     General idea:
     1. Initialize a = [], loop candies, if i is not in a, then add it in, and finally determine the length of len (a) * 2 and the length of candies 
    if len ( a) * 2 > len (candies), you need to return candies // 2 
    Otherwise, return len (a)
     '' '
     def distributeCandies (self, candies): 
        a = []
         for i in candies:
             if i not in a: 
                a.append (i) 
        
        if len (a) * 2 > len (candies):
             return candies //2 
        return len (a)

 

Guess you like

Origin www.cnblogs.com/yunxintryyoubest/p/12685162.html