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.

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

二、输入输出

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. 

三、解题思路

题目的意思是找到最多的不同的数字,这个个数不能超过总数字个数的一半。

  • 把原字符存储到map中并记录下每个数字出现的个数。依次遍历这个map,如果个数为正,那么类型数就加1 总个数也加1 类型数不会超过map的size 总个数不能超过原数组长度的一半。这两个判断条件是关键。
class Solution {
public:int distributeCandies(vector<int>& candies) {map<int, int> dict;for (auto ite : candies){if(dict.find(ite) == dict.end()) dict.insert(make_pair(ite, 1));else dict[ite]++;}int ret = 0, count = 0, tot = candies.size(), totKind = dict.size();for (auto ite : dict){if(ite.second > 0) {ite.second--;ret++;count++;}if(ret >= totKind || count >= tot/2)break;}return ret;}
};

LeetCode 575. Distribute Candies相关推荐

  1. LeetCode | 575. Distribute Candies

    题外话:又是N久没有更博,今后争取每天总结. Leetcode上的新题,难度是Easy,我是按照acceptance刷的,所以经常遇到新题^_________^ 题目: Given an intege ...

  2. 575. Distribute Candies*

    575. Distribute Candies* https://leetcode.com/problems/distribute-candies/ 题目描述 Given an integer arr ...

  3. []575. Distribute Candies

    problem 575. Distribute Candies 理解题意:主要是计算candies的种类数. 如果大于candies数目的一半,则返回一半的值,否则,返回candies的种类数. so ...

  4. 575. Distribute Candies 平均分糖果,但要求种类最多

    [抄题]: Given an integer array with even length, where different numbers in this array represent diffe ...

  5. Distribute Candies

    Distribute Candies int distributeCandies(vector& candies) { int cnt = 1;//初始有一种int N = candies.s ...

  6. C#LeetCode刷题之#575-分糖果​​​​​​​(Distribute Candies)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3798 访问. 给定一个偶数长度的数组,其中不同的数字代表着不同种 ...

  7. LeetCode 575. 分糖果(set集合去重)

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

  8. leetcode 575. 分糖果

    class Solution(object):def distributeCandies(self, candies):""":type candies: List[in ...

  9. leetcode 575 分糖果

    class Solution {public int distributeCandies(int[] candies) {int pre=-100001;Arrays.sort(candies);in ...

最新文章

  1. 「小程序JAVA实战」小程序头像图片上传(下)(45)
  2. Linux SPI总线和设备驱动架构之三:SPI控制器驱动
  3. Test435678
  4. arduino读取水位传感器的数据显示在基于i2c的1602a上_XSB-IC-S2智能水位监测仪-老友网...
  5. VS2013 VS2015应用程序无法正常启动0xc000007b。
  6. 用计算机如何扫描文件,打印机扫描文件到电脑方法教程
  7. 基于Django 文档1.11 自解+补完 学习django ---part1
  8. web 页面实现页面右下角弹窗功能
  9. [渝粤教育] 江西财经大学 中国会计准则(全英文) 参考 资料
  10. 后台java,前台JSP实现Excel导出(已经在后台查询,直接在前端实现下载,系统后台整体框架使用SSM)
  11. 解开神经科学中的交叉频率耦合
  12. css-浮动布局与样式规则
  13. 2017 icpc 西安赛区 B.Coin(推公式+二项式定理)
  14. Taro 项目里面添加 Mock 数据
  15. 基于SSM移动计费管理系统
  16. 操作系统原理,进程的基本状态,运行态,就绪态,等待态与转换模型,进程的其他状态,创建,终止,挂起与转换模型,Linux进程状态模型示例
  17. 光缆单盘检测与光缆线路测试需使用双窗口吗?
  18. max31865模块RTD测温注意事项
  19. android系统百度网盘传文件怎么打开,如何将手机百度网盘中下载的文件转移至电脑内...
  20. 吴恩达机器学习(十九)神经网络(I)

热门文章

  1. nessus安装成功后,出现“Establishing connection, please wait...”该怎么解决呀?
  2. IT工单系统到底有什么好处
  3. [VBA]VBA调用Python
  4. python象棋棋盘麦粒问题_Python求解“棋盘米粒倍增”问题
  5. vue中读取本地文件内容
  6. 利用tushare接口获取前一日新闻联播文字稿
  7. [文萃画材·油画]芭蕾舞女梦幻而绝美
  8. Python 装饰器解析
  9. python学英语视频教程_python教程(入门, 经典, 英文)
  10. 多功能计算器1.0.4