575. 分配糖果 Distribute Candies

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].

题意,给一个数字,长度为偶数,表示不同类型的糖果。需要把糖果平分给兄妹俩,求妹妹可以获得多少种类型的糖果

  1. public class Solution {
  2. public int DistributeCandies(int[] candies) {
  3. if(candies.Length == 0){
  4. return 0;
  5. }
  6. Dictionary<int, int> dict = new Dictionary<int, int>();
  7. for (int i = 0; i < candies.Length; i++) {
  8. int value = 0;
  9. int key = candies[i];
  10. dict.TryGetValue(key, out value);
  11. if (value > 0) {
  12. dict[key]++;
  13. } else {
  14. dict[key] = 1;
  15. }
  16. }
  17. return dict.Count >= candies.Length/2 ? candies.Length/2 : dict.Count;
  18. }
  19. }

null

时间: 2024-10-24 09:55:16

575. 分配糖果 Distribute Candies的相关文章

LeetCode.1103-向人们分发糖果(Distribute Candies to People)

这是小川的第393次更新,第425篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第256题(顺位题号是1103).我们通过以下方式向一排n = num_people个人分发一些糖果: 给第一个人送1个糖果,给第二个人送2个糖果,依此类推,直到我们给最后一个人送糖果.然后,我们回到行的开头,向第一个人提供n + 1个糖果,向第二个人提供n + 2个糖果,依此类推,直到我们向最后一个人提供2 * n个糖果. 这个过程重复进行,直到我们用完糖果.最后一个人将得到所有剩余的

欢乐暑假线上编程比赛第四题:分配糖果

在csdn上看到这么个题目,与友友们一起分享下,如果有别的做法,也希望能拿出来交流交流. 题目详情 有n个小朋友站成一排(编号从0到n-1),每个小朋友有一个rating值,存放在ratings数组中.老师需要给他们分 配糖果,每个小朋友至少需要一颗糖果,对于任意相邻的两个小朋友i和i+1,rating值大的必须比rating值小的分 配的糖果多(rating相同的没必要分配一样多的糖果). 请计算最少需要多少颗糖果,才能完成上述分配. 输入格式: 多组数据,每组数据第一行是一个正整数n. 接下

编程挑战高校俱乐部分配糖果答案

题目详情 有n个小朋友站成一排(编号从0到n-1),每个小朋友有一个rating值,存放在ratings数组中.老师需要给他们分配糖果,每个小朋友至少需要一颗糖果,对于任意相邻的两个小朋友i和i+1,rating值大的必须比rating值小的分配的糖果多(rating相同的没必要分配一样多的糖果). 请计算最少需要多少颗糖果,才能完成上述分配. 输入格式: 多组数据,每组数据第一行是一个正整数n. 接下来n行,每行有1个正整数,表示每个小朋友的rating值.所有整数都不超过100000. 输出

LeetCode 575. Distribute Candies (发糖果)

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.

575. Distribute Candies

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.

[LeetCode] 1103. Distribute Candies to People 分糖果

题目: 思路: 本题一开始的思路就是按照流程一步步分下去,算是暴力方法,在官方题解中有利用等差数列进行计算的 这里只记录一下自己的暴力解题方式 只考虑每次分配的糖果数,分配的糖果数为1,2,3,4,5,..., 依次加1 再考虑到分配的轮数,可以利用 i % num_people 来求得第i次应该分配到第几个人 最后要注意的是,如果当前糖果数小于本应该分配的糖果数,则将当前糖果全部给予,也就是要判断剩余糖果数 candies 与本该分配糖果数 i+1 的大小,谁小分配谁 代码: class So

LeetCode解题思路:575. Distribute Candies

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.

[LeetCode] Distribute Candies 分糖果

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.

LeetCode - 575. Distribute Candies

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.