[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. 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:
- 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].
这道题给我们一堆糖,每种糖的个数不定,分给两个人,让我们求其中一个人能拿到的最大的糖的种类数。那么我们想,如果总共有n个糖,平均分给两个人,每人得到n/2块糖,那么能拿到的最大的糖的种类数也就是n/2种,不可能再多,只可能再少。那么我们要做的就是统计出总共的糖的种类数,如果糖的种类数小于n/2,说明拿不到n/2种糖,最多能拿到的种类数数就是当前糖的总种类数,明白了这点就很容易了,我们利用集合set的自动去重复特性来求出糖的种类数,然后跟n/2比较,取二者之中的较小值返回即可,参加代码如下:
解法一:
class Solution {
public:
int distributeCandies(vector<int>& candies) {
unordered_set<int> s;
for (int candy : candies) s.insert(candy);
return min(s.size(), candies.size() / );
}
};
下面这种方法叼的不行,直接用把上面的解法浓缩为了一行,有种显摆的感觉:
解法二:
class Solution {
public:
int distributeCandies(vector<int>& candies) {
return min(unordered_set<int>(candies.begin(), candies.end()).size(), candies.size() / );
}
};
参考资料:
https://leetcode.com/problems/distribute-candies/
https://leetcode.com/problems/distribute-candies/discuss/102946/c-1-liner
https://leetcode.com/problems/distribute-candies/discuss/102939/java-8-one-line-solution-on
https://leetcode.com/problems/distribute-candies/discuss/102879/java-solution-3-lines-hashset
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Distribute Candies 分糖果的更多相关文章
- Leetcode575.Distribute Candies分糖果
给定一个偶数长度的数组,其中不同的数字代表着不同种类的糖果,每一个数字代表一个糖果.你需要把这些糖果平均分给一个弟弟和一个妹妹.返回妹妹可以获得的最大糖果的种类数. 示例 1: 输入: candies ...
- LeetCode Distribute Candies
原题链接在这里:https://leetcode.com/problems/distribute-candies/#/description 题目: Given an integer array wi ...
- 575. Distribute Candies 平均分糖果,但要求种类最多
[抄题]: Given an integer array with even length, where different numbers in this array represent diffe ...
- LeetCode之小孩分糖果
给定一群站好队的小孩而且按某项分值排名(姑且如果为年龄吧),年龄大的要比他身边年龄小的拿的糖要多.求怎么分配糖果使得分配的糖果数最少. 用一个数组从左到右再从右到左的遍历,向前遍历时若右边的比左边的大 ...
- LeetCode 1103. Distribute Candies to People (分糖果 II)
题目标签:Math 题目让我们分发糖果,分的糖果从1 开始依次增加,直到分完. for loop可以计数糖果的数量,直到糖果发完.但是还是要遍历array 给people 发糖,这里要用到 index ...
- [LeetCode] Candy (分糖果),时间复杂度O(n),空间复杂度为O(1),且只需遍历一次的实现
[LeetCode] Candy (分糖果),时间复杂度O(n),空间复杂度为O(1),且只需遍历一次的实现 原题: There are N children standing in a line. ...
- LeetCode 1103. Distribute Candies to People
1103. Distribute Candies to People(分糖果||) 链接:https://leetcode-cn.com/problems/distribute-candies-to- ...
- Java实现 LeetCode 575 分糖果(看看是你的长度小还是我的种类少)
575. 分糖果 给定一个偶数长度的数组,其中不同的数字代表着不同种类的糖果,每一个数字代表一个糖果.你需要把这些糖果平均分给一个弟弟和一个妹妹.返回妹妹可以获得的最大糖果的种类数. 示例 1: 输入 ...
- LeetCode:135. 分发糖果
LeetCode:135. 分发糖果 老师想给孩子们分发糖果,有 N 个孩子站成了一条直线,老师会根据每个孩子的表现,预先给他们评分. 你需要按照以下要求,帮助老师给这些孩子分发糖果: 每个孩子至少分 ...
随机推荐
- [luogu2831][noip d2t3]愤怒的小鸟_状压dp
愤怒的小鸟 noip-d2t3 luogu-2831 题目大意:给你n个点,问最少需要多少条经过原点的抛物线将其覆盖. 注释:1<=点数<=18,1<=数据组数<=30.且规定 ...
- cocos对lua代码加密
1.0 cocos luacompile 用法 我用的普通的cocos2d lua,没用quick,quick好像可以对整个资源包括图像和音频都加密,打包成zip.但我没用quick.看了下luaco ...
- Notepad++中实现Markdown语法高亮与实时预览
Notepad ++是一个十分强大的编辑器,除了可以用来制作一般的纯文字说明文件,也十分适合编写计算机程序代码.Notepad ++不仅有语法高亮度显示,也有语法折叠功能,并且支持宏以及扩充基本功能的 ...
- C语言——第二次作业(2)
作业要求一 PTA作业的提交列表 作业要求二 题目1.删除字符串中数字字符(函数题) 1.设计思路 - (1)算法 第一步:调用定义的函数. 第二步:定义i=0.j=0,i为原字符数组角标,j为删除后 ...
- python 单向链表实现
单链表的操作 is_empty() 链表是否为空 length() 链表长度 travel() 遍历整个链表 add(item) 链表头部添加元素 append(item) 链表尾部添加元素 inse ...
- zookeeper 启动失败 BindException: Address already in use 或者Error contacting service. It is probably not running
平台:centos-6.3-i386 jdk-7u51 storm 0.9.1 python 2.6.6 hadoop 1.2.1 今天上午装storm的时候遇到这个问题,好郁闷.把网上介绍的方法 ...
- css3动画transition详解
一.transition-property 语法: transition-property : none | all | [ <IDENT> ] [ ',' <IDENT> ] ...
- HttpWebRequest,HttpWebResponse C# 代码调用webservice,参数为xml
先上调用代码 public static string PostMoths(string url, string Json) { System.Net.HttpWebRequest request; ...
- 从一个事件绑定说起 - DOM
事件绑定的方式 给 DOM 元素绑定事件分为两大类:在 html 中直接绑定 和 在 JavaScript 中绑定. Bind in HTML 在 HTML 中绑定事件叫做内联绑定事件,HTML 的元 ...
- 返回到前台的String出现乱码问题
使用springmvc给前天返回String类型的数据出现乱码问题可以在配置环境Spring-mvc.xml中添加如下代码 <mvc:annotation-driven> <mvc: ...