题目要求
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.
题目分析及思路
给定一个长度为偶数的整数数组,其中不同数字代表不同种类的糖果,且一个数字代表一颗该种糖果。需要将这些糖果平均分给男生和女生,要求返回女生能得到糖果种类的最大值。可以先利用集合特性获得糖果种类数及女生数量,若种类数少于女生数量,则返回种类数;否则返回女生数量。
python代码
class Solution:
def distributeCandies(self, candies: List[int]) -> int:
candy_kinds = len(set(candies))
bro_or_sis_num = len(candies)//2
if candy_kinds < bro_or_sis_num:
return candy_kinds
else:
return bro_or_sis_num