[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 个孩子站成了一条直线,老师会根据每个孩子的表现,预先给他们评分. 你需要按照以下要求,帮助老师给这些孩子分发糖果: 每个孩子至少分 ...
随机推荐
- hihocoder [Offer收割]编程练习赛52 D 部门聚会
看了题目的讨论才会做的 首先一点,算每条边(u, v)对于n*(n+1)/2种[l, r]组合的贡献 正着算不如反着算 哪些[l, r]的组合没有包含这条边(u, v)呢 这个很好算 只需要统计u这半 ...
- Dynamics 365 for CRM: Sitemap站点图的可视化编辑功能
Dynamics 365 for CRM 提供了Sitemap站点图的可视化编辑功能 在之前的所有版本中,我们只能通过从系统中导出站点图的XML进行编辑后再导入(容易出错),或使用第三方的Sitema ...
- lua 二维数组创建
local arr= {} for i=1, 4 do arr[i] = {} end 使用时可以直接使用arr[i][j]
- Java读取文件存储到mysql
写了一批Lua脚本,要放到数据库里面,调用的时候进行计算,由于有太多lua脚本,就写了个程序来录入. 下面主要分三个部分: public static String readToString(Stri ...
- beta冲刺7
前言:最后一篇惹.明天就是正式交差了.有点慌-- 昨天的未完成: 用户试用+测评 输入部分的正则式判定 今天的工作: 登陆界面修改 我的社团显示效果优化 部分信息注册后锁定无法修改 其他部分功能优化 ...
- beta冲刺1
前言:这篇算是开始补之前的开端,毕竟beta阶段我们从前面开始就有在陆续做了. 今天的工作: 接收了新成员*1,然后几个人聚了一下,并且讨论了一下目前遇到的问题,以及目前需要处理的问题. 目前遇到的问 ...
- css变化代码
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> ...
- 关于Android 7.0(API24)相机的问题汇总
在开发Android项目的时候,我们会用到相机,有些时候只是开发一个普通的扫码,仅仅赋予一下 权限 就好了,但是有些时候是需要拍照和从相册中获取照片的.我们在Android 5.0以及5.0之前调用相 ...
- 改变input的placeholder颜色
input::-webkit-input-placeholder{ color:#666; } input::-ms-input-placeholder{ color:#666; } input::- ...
- tcltk控制chariot进行测试 couldn't load library "ChariotExt": invalid argument
解决办法:和tcl版本有关,我的chariot应该是32位的,下载win32-ix86的tcl解决了,用64位的有这个错误提示. ActiveTcl8.6.4.1.299124-win32-ix86- ...