[抄题]:

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. 

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 查看set中有几个数,只要用.size()即可

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

hashtable hashset的key都具有单一性 不能重复

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

hashset的key具有单一性 不能重复,对应糖果的种类也是

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

class Solution {public int distributeCandies(int[] candies) {//ccif (candies == null || candies.length == 0) {return 0;}//iniSet kinds = new HashSet();//forfor (int candy : candies) {kinds.add(candy);}//return :return (candies.length / 2) > kinds.size() ? kinds.size() : (candies.length / 2);}
}

View Code

转载于:https://www.cnblogs.com/immiao0319/p/8931800.html

575. Distribute Candies 平均分糖果,但要求种类最多相关推荐

  1. 575. Distribute Candies*

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

  2. []575. Distribute Candies

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

  3. LeetCode 575. Distribute Candies

    575. Distribute Candies 一.问题描述 Given an integer array with even length, where different numbers in t ...

  4. LeetCode | 575. Distribute Candies

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

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

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

  6. Distribute Candies

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

  7. 众所周知亚马逊是全球最大的在线电子商务公司。如今,它已成为全球商品种类最多的在线零售商,日活跃买家约为20-25亿。另一方面,也有大大小小的企业,但不是每个人都能赚到刀! 做网店的同学都知道,

    众所周知亚马逊是全球最大的在线电子商务公司.如今,它已成为全球商品种类最多的在线零售商,日活跃买家约为20-25亿.另一方面,也有大大小小的企业,但不是每个人都能赚到刀! 做网店的同学都知道,对于网店 ...

  8. Leetcode575(力扣575):分糖果

    使用set自动去重的特性统计糖果的种类,然后返回种类和一半糖果数较小的那个即可 class Solution {public:int distributeCandies(vector<int&g ...

  9. leetcode刷题笔记(3)(python)

    485 Max Consecutive Ones 题意:给出一串二进制数组,求数组中最长的连续1的个数 思路:遍历数组判断,然后将值添加到长度保存数组中,取保存数组最大值.本题要考虑输入的数组为[0] ...

最新文章

  1. 4-spark学习笔记-spark运行模式与原理
  2. 栈的应用--进制转换
  3. 【机器学习】集成模型方法
  4. python(numpy,pandas9)——pandas 导入导出数据
  5. 精度,精确率,召回率_了解并记住精度和召回率
  6. Java讲课笔记21:List接口及其实现类
  7. C#接口的使用场合,接口应用
  8. NYOJ 82:迷宫寻宝(一)(BFS)
  9. 微信点餐系统感悟(上1-6章)
  10. dev多行注释_Shell 实现多行注释的几种常用方法
  11. Spring——依赖注入的三种方式
  12. icem搅拌器网格划分_搅拌器研究所的第六个开放电影项目
  13. 【数码复印新生态】东芝泰格强势支持统信UOS
  14. 树洞程序php,微信公众平台开发(65) 微博树洞_PHP教程
  15. 面试——HR问你的职业规划,应该怎样回答?
  16. 计算机c盘突然少了几个G,做系统时c盘显示0容量-关于Windows系统c盘突然没了十几个g...
  17. 模拟复杂红绿灯交通指示程序编程显示黄灯闪烁箭头指示
  18. Java继承和多态(保姆级教学)
  19. torch.nn.parameter详解
  20. 三菱fx2n64mr说明书_三菱FX2N-64MR-UA1/UL使用说明书FX2N-64MR-UA1/UL手册 - 广州凌控

热门文章

  1. linux centos7不能ssh免密登录自己的问题解决
  2. 【收藏】联想官方出品一键关闭Windows自动更新工具,和Windows自动更新说再见
  3. Go语言web框架beego:目录说明
  4. Python3 数字类型转换
  5. Redis配置文件常用配置详解
  6. MySQL设置数据库的字符编码为utf8
  7. MySQL 避免行锁升级为表锁——使用高效的索引
  8. java file类包_Java中File类的常用API
  9. android os于8.1区别,Android-x86 8.1-rc2发布 运行于x86 PC上的安卓OS
  10. java 获取400的错误信息_获取400错误的请求Spring RestTemplate POST