问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3798 访问。

给定一个偶数长度的数组,其中不同的数字代表着不同种类的糖果,每一个数字代表一个糖果。你需要把这些糖果平均分给一个弟弟和一个妹妹。返回妹妹可以获得的最大糖果的种类数。

输入: candies = [1,1,2,2,3,3]

输出: 3

解析: 一共有三种种类的糖果,每一种都有两个。

最优分配方案:妹妹获得[1,2,3],弟弟也获得[1,2,3]。这样使妹妹获得糖果的种类数最多。

输入: candies = [1,1,2,3]

输出: 2

解析: 妹妹获得糖果[2,3],弟弟获得糖果[1,1],妹妹有两种不同的糖果,弟弟只有一种。这样使得妹妹可以获得的糖果种类数最多。

注意:

数组的长度为[2, 10,000],并且确定为偶数。

数组中数字的大小在范围[-100,000, 100,000]内。


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.

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.

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:

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

The number in given array is in range [-100,000, 100,000].


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3798 访问。

public class Program {

    public static void Main(string[] args) {
var words = new int[] { 1, 1, 2, 2, 3, 3 }; var res = DistributeCandies(words);
Console.WriteLine(res); Console.ReadKey();
} public static int DistributeCandies(int[] candies) {
var res = new HashSet<int>();
for(var i = 0; i < candies.Length; i++) {
if(!res.Contains(candies[i])) {
res.Add(candies[i]);
}
}
if(res.Count < candies.Length / 2) {
return res.Count;
}
return candies.Length / 2;
} }

以上给出1种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3798 访问。

3

分析:

显而易见,以上算法的时间复杂度为: 

C#LeetCode刷题之#575-分糖果​​​​​​​(Distribute Candies)的更多相关文章

  1. C#LeetCode刷题之#704-二分查找(Binary Search)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3999 访问. 给定一个 n 个元素有序的(升序)整型数组 num ...

  2. [Swift]LeetCode575. 分糖果 | Distribute Candies

    Given an integer array with even length, where different numbers in this array represent different k ...

  3. C#LeetCode刷题-二分查找​​​​​​​

    二分查找篇 # 题名 刷题 通过率 难度 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)-该题未达最优解 30 ...

  4. LeetCode刷题的一点个人建议和心得

    目录 1.    为什么我们要刷LeetCode? 2.    LeetCode的现状和问题 3.    本文的初衷 4.    LeetCode刷题建议 4.1入门数据结构,打基础阶段 4.2 建立 ...

  5. Java实现 LeetCode 575 分糖果(看看是你的长度小还是我的种类少)

    575. 分糖果 给定一个偶数长度的数组,其中不同的数字代表着不同种类的糖果,每一个数字代表一个糖果.你需要把这些糖果平均分给一个弟弟和一个妹妹.返回妹妹可以获得的最大糖果的种类数. 示例 1: 输入 ...

  6. C#LeetCode刷题-哈希表

    哈希表篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 42.8% 简单 3 无重复字符的最长子串   24.2% 中等 18 四数之和   ...

  7. LeetCode刷题总结之双指针法

    Leetcode刷题总结 目前已经刷了50道题,从零开始刷题学到了很多精妙的解法和深刻的思想,因此想按方法对写过的题做一个总结 双指针法 双指针法有时也叫快慢指针,在数组里是用两个整型值代表下标,在链 ...

  8. LeetCode刷题总结-数组篇(上)

    数组是算法中最常用的一种数据结构,也是面试中最常考的考点.在LeetCode题库中,标记为数组类型的习题到目前为止,已累计到了202题.然而,这202道习题并不是每道题只标记为数组一个考点,大部分习题 ...

  9. LeetCode刷题总结-数组篇(中)

    本文接着上一篇文章<LeetCode刷题总结-数组篇(上)>,继续讲第二个常考问题:矩阵问题. 矩阵也可以称为二维数组.在LeetCode相关习题中,作者总结发现主要考点有:矩阵元素的遍历 ...

  10. LeetCode刷题总结-树篇(中)

    本篇接着<LeetCode刷题总结-树篇(上)>,讲解有关树的类型相关考点的习题,本期共收录17道题,1道简单题,10道中等题,6道困难题. 在LeetCode题库中,考察到的不同种类的树 ...

随机推荐

  1. Mock分页

    前后端分离开发时,一般会使用mock. 因为mock是用node运行的,行为与调用后台一致. 这样,不需要等后台写好,只要有接口文档,前端可以自己调接口,这样联调时遇到的问题会少很多,可以加快整体开发 ...

  2. Flarum 的安装与配置

    Flarum 是一款非常棒的开源论坛程序,本鸽子的论坛 就是用 Flarum 搭建的.之前有人问过我 Flarum 如何搭建,所以下面讲一下 Flarum 的搭建过程. 前提 域名需要提前解析. 有一 ...

  3. 哈夫曼编码+python实现

    关于哈夫曼树怎么构建的.哈夫曼编码怎么求,请参考 哈夫曼树及python实现 这些基础的东西就不在这里阐述了,本文直接上代码. 参考链接:哈夫曼树的 Python 实现 哈夫曼树的构建和编码 ''' ...

  4. HDU-2473 Junk-Mail Filter(并查集的使用)

    原题链接:https://vjudge.net/problem/11782/origin Description: Recognizing junk mails is a tough task. Th ...

  5. linux上安装mysql 5.7.22

    主要步骤可以参照该网址: https://www.cnblogs.com/jxrichar/p/9248480.html 这里记录一下自己遇到的问题 1.在配置 vim /etc/my.cnf 文件的 ...

  6. java实现单链表的增删改以及排序

    使用java代码模拟单链表的增删改以及排序功能 代码如下: package com.seizedays.linked_list; public class SingleLinkedListDemo { ...

  7. Python延迟初始化(lazy property)

    转自:https://blog.csdn.net/azsx02/article/details/77649527 Python 对象的延迟初始化是指,当它第一次被创建时才进行初始化,或者保存第一次创建 ...

  8. 使用faker生成测试数据

    需要先安装faker模块,pip install faker 导入模块中的Faker类:from faker import Faker 实例化faker = Faker() print('姓名相关') ...

  9. Python大礼包-安装视频+pycharm编译器|Mac版本+64位+32位版本pycharm安装包+python安装|内附网盘链接带提取码

    pycharm安装包+环境安装打包带走,附带视频教程与pdf教程. (下载链接在本文最下方) 多的不说,直接上图: Python大礼包-安装视频+pycharm编译器详细文件: 点击此处进入下载地址 ...

  10. luogu P4525 自适应辛普森法1

    LINK:自适应辛普森法1 观察题目 这个东西 凭借我们的数学知识应该是化简不了的. 可以直接认为是一个函数 求定积分直接使用辛普森就行辣. 一种写法: double a,b,c,d; double ...