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/ 题目比较长,总结起来很简单:有个整型数组,长度是偶数,把它分成两份,要求有一 ...
 
随机推荐
- 如何快速上手.net下单元测试工具NUnit?
			
NUnit基本使用 准备知识: 读此博文需要了解单元测试基本概念及NUnit的的安装. 传送门:单元测试之道(使用NUnit) 1.常见的错误 当学习一个新东西时,先学习错误,是最快的方式. 1.1 ...
 - N位N进制里有多少个N
			
32位二进制里有多少个1 https://blog.csdn.net/zhangsj1007/article/details/81411063 有这样一道计算机问题"32位二进制里面有多少个 ...
 - 剑指offer-第六章面试中的各项能力(翻转单词的顺序VS左旋转字符串)
			
//题目1:翻转单词顺序例如“Hello world!”翻转后为world! Hello. //思路:首先翻转整个字符串,然后再分别翻转每个单词. //题目2:左旋转字符串,是将字符串的前面几个(n) ...
 - Instruments检测解决内存泄露以及进行性能测试
			
1.启动Xcode自带的Instruments.这里有两种方法启动. 方法一: 方法二: 2.选择Leaks选项.(该选项用来进行内存泄漏检测) 说明: Leaks:找到引发内存泄漏的起点. Time ...
 - 【ftp】服务器的链接命令
			
1. 连接ftp服务器 格式:ftp [hostname| ip-address] a)在Linux命令行下输入: ftp 192.168.1.1 b)服务器询问你用户名和密码,分别输入用户名和相应密 ...
 - lvs之  lvs+nginx+tomcat_1、tomcat_2+redis(lvs dr 模式)
			
前提:已经安装好 lvs+nginx+tomcat_1.tomcat_2+redis环境 ,可参考 (略有改动,比如tomcat_1.tomcat_2安装在两台机器上,而不是单机多实例 ,自行稍稍变动 ...
 - (转)Android和JavaScript互相调用
			
Html页面和Java代码结合的方式一般用在界面经常被更改 的情况下,可以讲html放在网络中,软件一打开就会访问网络获取到最新的界面.缺点是会受到网络信号的影响,从而导致访问速度慢. 1.用WebV ...
 - Linux环境抓包命令
			
有时候有些接口调用问题不好定位或者日志不够详细,那么我们往往会选择通过抓包来看详细的通讯过程.抓包有相关软件,这里说如何直接在环境里抓.假如现在我们在 Linux 下部署了 Tomcat 服务器,端口 ...
 - 简述FPGA时序约束理论
			
FPGA时序约束简介. 时序约束的场景: 在简单电路中,当频率较低时,数字信号的边沿时间可以忽略时,无需考虑时序约束.但在复杂电路中,为了减少系统中各部分延时,使系统协同工作,提高运行频率,需要进行时 ...
 - PhysicalDrive
			
由于"\"是C/C+中转义符, "\\\\.\\"就相当于\\.\ 在Windows中 \\.\ 前缀用于标识设备,其中的"."表示本地计算 ...