575. Distribute Candies

本文探讨了一个有趣的问题:如何将不同种类的糖果平均分配给兄妹两人,并确保妹妹能获得尽可能多的不同种类的糖果。通过使用Python中的set函数,我们找到了一个简洁而高效的解决方案。
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: 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. 

Example 2:

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. 

Note:

  1. The length of the given array is in range [2, 10,000], and will be even.
  2. The number in given array is in range [-100,000, 100,000].

解题思路:

    结果只有两种可能,要么是数组的长度的一半或者所有不重复的元素的个数,这两个谁小答案就是哪一个,通过使用set函数可以得到没有重复元素的candies的数组。

解题代码:

    

return min(len(candies)/2,len(set(candies)))
        



Problem Statement You have an unlimited supply of two types of candies: small candies and large candies. The weight of a small candy is X grams, and the weight of a large candy is Y grams. Large candies are heavier than small candies (that is, X<Y). There are N children, numbered 1 to N. You have decided to distribute candies so that the following conditions are satisfied: For i=1,…,N, child i receives exactly A i ​ candies in total of the two types. The total weights of candies distributed to the N children are all equal. Determine whether there exists a distribution method that satisfies the conditions. If it exists, find the maximum possible value for the number of large candies distributed. DeepL 翻译    问题陈述 你有无限量的两种糖果:小糖果和大糖果。小糖果的重量是 X 克,大糖果的重量是 Y 克。大糖果比小糖果重(即 X<Y )。 有 N 个孩子,编号为 1 至 N 。 你决定分发糖果,以满足以下条件: 对于 i=1,…,N 来说, i 个孩子收到的两种糖果的总重量正好是 A i ​ 。 分给 N 个孩子的糖果的总重量都相等。 确定是否存在满足条件的分配方法。如果存在,求大号糖果分配数量的最大可能值。    Constraints 2≤N≤2×10 5 1≤A i ​ ≤10 9 1≤X<Y≤10 9 All input values are integers. DeepL 翻译    限制因素 2≤N≤2×10 5 1≤A i ​ ≤10 9 1≤X<Y≤10 9 所有输入值均为整数。    Input The input is given from Standard Input in the following format: N X Y A 1 ​ … A N ​ DeepL 翻译    输入 输入内容由标准输入法提供,格式如下 N X Y A 1 ​ … A N ​    Output If there is no distribution method that satisfies the conditions, output -1. If there exists a distribution method that satisfies the conditions, output the maximum possible value for the number of large candies distributed in such a distribution method. DeepL 翻译    输出 如果没有满足条件的分配方法,则输出 -1。 如果存在满足条件的分配方法,则输出这种分配方法所分配的大颗糖果数量的最大可能值。
11-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值