C#LeetCode刷题之#575-分糖果(Distribute Candies)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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)的更多相关文章
- C#LeetCode刷题之#704-二分查找(Binary Search)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3999 访问. 给定一个 n 个元素有序的(升序)整型数组 num ...
- [Swift]LeetCode575. 分糖果 | Distribute Candies
Given an integer array with even length, where different numbers in this array represent different k ...
- C#LeetCode刷题-二分查找
二分查找篇 # 题名 刷题 通过率 难度 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)-该题未达最优解 30 ...
- LeetCode刷题的一点个人建议和心得
目录 1. 为什么我们要刷LeetCode? 2. LeetCode的现状和问题 3. 本文的初衷 4. LeetCode刷题建议 4.1入门数据结构,打基础阶段 4.2 建立 ...
- Java实现 LeetCode 575 分糖果(看看是你的长度小还是我的种类少)
575. 分糖果 给定一个偶数长度的数组,其中不同的数字代表着不同种类的糖果,每一个数字代表一个糖果.你需要把这些糖果平均分给一个弟弟和一个妹妹.返回妹妹可以获得的最大糖果的种类数. 示例 1: 输入 ...
- C#LeetCode刷题-哈希表
哈希表篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 42.8% 简单 3 无重复字符的最长子串 24.2% 中等 18 四数之和 ...
- LeetCode刷题总结之双指针法
Leetcode刷题总结 目前已经刷了50道题,从零开始刷题学到了很多精妙的解法和深刻的思想,因此想按方法对写过的题做一个总结 双指针法 双指针法有时也叫快慢指针,在数组里是用两个整型值代表下标,在链 ...
- LeetCode刷题总结-数组篇(上)
数组是算法中最常用的一种数据结构,也是面试中最常考的考点.在LeetCode题库中,标记为数组类型的习题到目前为止,已累计到了202题.然而,这202道习题并不是每道题只标记为数组一个考点,大部分习题 ...
- LeetCode刷题总结-数组篇(中)
本文接着上一篇文章<LeetCode刷题总结-数组篇(上)>,继续讲第二个常考问题:矩阵问题. 矩阵也可以称为二维数组.在LeetCode相关习题中,作者总结发现主要考点有:矩阵元素的遍历 ...
- LeetCode刷题总结-树篇(中)
本篇接着<LeetCode刷题总结-树篇(上)>,讲解有关树的类型相关考点的习题,本期共收录17道题,1道简单题,10道中等题,6道困难题. 在LeetCode题库中,考察到的不同种类的树 ...
随机推荐
- “我放弃了年薪20万的offer…”
最近身边朋友换工作.转型的越来越多.爬到一定高度,或者说到了一定年龄,每个选择都显得尤为重要.不仅因为高昂的机会成本,还有大家对后续规划的多重考虑.有一个说法你可能听过:混职场,要拥有不可替代的能力. ...
- Jenkins链接Kubernetes集群
Jenkins CI/CD介绍 持续构建与发布是我们工作中必不可少的一个步骤,目前大多公司都采用Jenkins集群来搭建符合需求的CI/CD流程,然而传统的Jenkins Slave一主多从方式会存在 ...
- Mybatis核心模块简介
Configuration 主要字段 Environment:配置DataSource和TransactionFactory ObjectFactory:bean工厂 MapperRegistry:M ...
- Linux切换用户时报错/.bash_profile: Permission denied,命令行(终端提示符)出现-bash-4.2$
Linux切换用户时报错/.bash_profile: Permission denied,命令行(终端提示符)出现-bash-4.2$ 利用su - 切换用户时,发现有一个用户切时出现如下情况 [r ...
- BUUCTF-web web1 (无列名注入)
注册并登录后发现,sql注入,注入点在广告申请的界面.加单引号发现报错 先通过insert插入数据,然后再通过id查询相应的数据,所以是二次注入. 常见报错函数updatexml,floor以及ext ...
- 【CVPR2020】Wavelet Integrated CNNs for Noise-Robust Image Classification
深度学习中的下采样(max-pooing, average-pooling, strided-convolution)通常会有两个不足:破坏了目标的基本结构.放大随机噪声.上采样操作同样容易受到影响. ...
- Eclipse创建Web项目后新建Servlet时报红叉错误 or 导入别人Web项目时报红叉错误 的解决办法
如图,出现类似红叉错误. 1.在项目名称上点击右键->Build Path->Configure Build Path 2.在弹出来的框中点击Add Library,如图 3.接下来选择U ...
- ES6 常用语法知识汇总
ES6模块化如何使用,开发环境如何打包? 1.模块化的基本语法 /* export 语法 */ // 默认导出 export default { a: '我是默认导出的', } // 单独导出 exp ...
- Python 访问字符串中的值
Python 访问字符串中的值 Python 不支持单字符类型,单字符在 Python 中也是作为一个字符串使用.高佣联盟 www.cgewang.com Python 访问子字符串,可以使用方括号来 ...
- 一本通 高手训练 1788 爬山 dp 斜率 凸包
LINK:爬山 很早以前看的题目 发现自己想的完全不对 这道题还是比较有价值的. 先不考虑走的路线问题 考虑某个点能看到的最高的山. 分左边和右边来考虑 考虑左边 利用单调栈存长度单调递减的山 不能直 ...