class Solution(object):
def distributeCandies(self, candies):
"""
:type candies: List[int]
:rtype: int
"""
myset=set()#表示多少种类
for i in candies:
if i not in myset:
myset.add(i)
if len(myset) is len(candies)//2:
break
return min(len(myset),len(candies)//2)注意,最后要取最小值,即假设 即使1000种不同的糖果,最后也要返回最小值
leetcode 575. 分糖果
最新推荐文章于 2024-04-03 23:45:00 发布
本文介绍了一种用于分配多种类型糖果的算法。通过使用集合来记录不同类型的糖果,该算法确保了每个人能够获得尽可能多的不同种类的糖果。最终返回的数值是最小的糖果种类数或每人能获得的糖果数。
304

被折叠的 条评论
为什么被折叠?



