575. Distribute Candies

本文介绍了一个关于糖果分配的问题,给出了一段C#代码实现。该问题要求将不同种类的糖果平均分配给兄妹两人,并返回妹妹能获得的最大糖果种类数。通过使用HashSet来存储糖果种类,确保了每种糖果只被计数一次。

原题

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. 

代码实现

       public int DistributeCandies(int[] candies)
        {
            int n = candies.Length;
            HashSet<int> kinds = new HashSet<int>();
            foreach (var item in candies)
            {
                if (!kinds.Contains(item))
                    kinds.Add(item);
            }
            if (kinds.Count > n/2)
                return n/2; //糖果要均分,所以种类再多,也只能分到n/2种
            return kinds.Count; //种类较少,妹妹最多分到kinds.Count种,如[1,1,1,1], 返回1
        }

leetcode-solution库

leetcode算法题目解决方案每天更新在github库中,欢迎感兴趣的朋友加入进来,也欢迎star,或pull request。https://github.com/jackzhenguo/leetcode-csharp

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、付费专栏及课程。

余额充值