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.
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]:
[思维问题]:
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
- 查看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) {
//cc
if (candies == null || candies.length == 0) {
return 0;
}
//ini
Set kinds = new HashSet();
//for
for (int candy : candies) {
kinds.add(candy);
}
//return :
return (candies.length / 2) > kinds.size() ? kinds.size() : (candies.length / 2);
}
}
575. Distribute Candies 平均分糖果,但要求种类最多的更多相关文章
- Leetcode575.Distribute Candies分糖果
给定一个偶数长度的数组,其中不同的数字代表着不同种类的糖果,每一个数字代表一个糖果.你需要把这些糖果平均分给一个弟弟和一个妹妹.返回妹妹可以获得的最大糖果的种类数. 示例 1: 输入: candies ...
- LeetCode 575. Distribute Candies (发糖果)
Given an integer array with even length, where different numbers in this array represent different k ...
- LeetCode 575 Distribute Candies 解题报告
题目要求 Given an integer array with even length, where different numbers in this array represent differ ...
- 【leetcode】575. Distribute Candies
原题 Given an integer array with even length, where different numbers in this array represent differen ...
- 【LeetCode】575. Distribute Candies 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- [LeetCode] Distribute Candies 分糖果
Given an integer array with even length, where different numbers in this array represent different k ...
- [LeetCode&Python] Problem 575. Distribute Candies
Given an integer array with even length, where different numbers in this array represent different k ...
- LeetCode: 575 Distribute Candies(easy)
题目: Given an integer array with even length, where different numbers in this array represent differe ...
- 575. Distribute Candies
https://leetcode.com/problems/distribute-candies/description/ 题目比较长,总结起来很简单:有个整型数组,长度是偶数,把它分成两份,要求有一 ...
随机推荐
- Nodejs实现爬虫抓取数据
开始之前请先确保自己安装了Node.js环境,还没有安装的的童鞋请自行百度安装教程...... 1.在项目文件夹安装两个必须的依赖包 npm install superagent --save-dev ...
- 【Python系统学习】基础篇
这次真的是最后一次了!第三次滚Python的基础.走了太多弯路.认真一点!菜鸟! 教程 转义字符 \ 可以转义很多字符,比如\n表示换行,\t表示制表符,字符\本身也要转义,所以\\表示的字符就是\ ...
- ASP.NET的几个试题(《C#与.NET程序员面试宝典》)
更多参考:博客园笔记 :ASP.NET是什么 ASP.NET不是一种语言,而是创建动态Web页的一种强大的服务器端技术,它是Microsoft.NET Framework中一套用于生成Web应用程序和 ...
- centos6.5 安装sftp
1.创建sftp组 : groupadd sftp 2.创建一个sftp用户,用户名为andy : useradd -g sftp -s /bin/false andy 3.修改密码: pas ...
- 在Spring中通过构造自动装配--constructor
在Spring中,可以使用“通过构造自动装配”,实际上是按构造函数的参数类型自动装配. 这意味着,如果一个bean的数据类型与其他bean的构造器参数的数据类型是相同的,那么将自动装配. packag ...
- Google搜索被屏蔽,如何使用Google搜索
我们在国内使用搜索引擎最多的是Google和Baidu啦,在引擎上找一些我们需要的知识,最近好像www.google.cn已经无法访问了,并且香港的链接www.google.com.hk也无法访问了, ...
- Ambari的API调用
GET api/v1/clusters/HDP/configurations可以获得所有的配置信息(例如,http://hdp0:8080/api/v1/clusters/HDP/configurat ...
- 异常:org.springframework.http.converter.HttpMessageNotReadableException
spring(springboot.springmvc)出现标题的异常一般是由于controller的入参失败引起的. 介绍下常规入参的两种格式: ,这种方式的入参主要是接受key-value的参数, ...
- 20位活跃在Github上的国内技术大牛
登录|注册 leon-这个程序员不闷骚的博客 喜欢leon,有追求有原则有爱心的杀手,做一个有追求的程序员,代码是程序员的朋友,虽然没有热情,但是非常忠实.希望拥有一身绝世武功,再配一把绝世好 ...
- 过河卒(Noip2002)(dp)
过河卒(Noip2002) 时间限制: 1 Sec 内存限制: 128 MB提交: 7 解决: 6[提交][状态][讨论版][命题人:quanxing] 题目描述 棋盘上A点有一个过河卒,需要走到 ...