LeetCode 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.
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].
Java Solution:
Runtime beats 90.78%
完成日期:06/07/2017
关键词:HashMap
关键点:分析可能性
public class Solution
{
public int distributeCandies(int[] candies)
{
HashSet<Integer> set = new HashSet<>(); for(int i=0; i<candies.length; i++)
set.add(candies[i]); // if candy number for sister >= the number of kinds for candies -> kinds
// if candy number for sister < the number of kinds for candies -> candy number
return Math.min(set.size(), candies.length/2);
}
}
参考资料:N/A
LeetCode 575. Distribute Candies (发糖果)的更多相关文章
- LeetCode 575 Distribute Candies 解题报告
题目要求 Given an integer array with even length, where different numbers in this array represent differ ...
- 575. Distribute Candies 平均分糖果,但要求种类最多
[抄题]: Given an integer array with even length, where different numbers in this array represent diffe ...
- LeetCode: 575 Distribute Candies(easy)
题目: Given an integer array with even length, where different numbers in this array represent differe ...
- LeetCode 1103. Distribute Candies to People
1103. Distribute Candies to People(分糖果||) 链接:https://leetcode-cn.com/problems/distribute-candies-to- ...
- LeetCode 1103. Distribute Candies to People (分糖果 II)
题目标签:Math 题目让我们分发糖果,分的糖果从1 开始依次增加,直到分完. for loop可以计数糖果的数量,直到糖果发完.但是还是要遍历array 给people 发糖,这里要用到 index ...
- [LeetCode] 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 differen ...
- 【LeetCode】575. Distribute Candies 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- [LeetCode&Python] Problem 575. Distribute Candies
Given an integer array with even length, where different numbers in this array represent different k ...
随机推荐
- Java SpringMVC小白的成长(一)
如果你是一个小白,请跟着我走,我会让你少走弯路,如果你是大牛,那么多谢大牛可以给我提提建议. 说实话,来公司这么久,一直在做的是维护与修改bug.(我的语言是php,来公司才开始接触java). 要毕 ...
- 使用IntelliJ IDEA查看类的继承关系图形
最近正好也没什么可忙的,就回过头来鼓捣过去的知识点,到Servlet部分时,以前学习的时候硬是把从上到下的继承关系和接口实现记得乱七八糟. 这次利用了IDEA的diagram,结果一目了然,也是好用到 ...
- Ningx集群环境搭建
Ningx集群环境搭建 Nginx是什么? Nginx ("engine x") 是⼀个⾼性能的 HTTP 和 反向代理 服务器,也是⼀个 IMAP/ POP3/SMTP 代理服务 ...
- Akka(26): Stream:异常处理-Exception handling
akka-stream是基于Actor模式的,所以也继承了Actor模式的“坚韧性(resilient)”特点,在任何异常情况下都有某种整体统一的异常处理策略和具体实施方式.在akka-stream的 ...
- Manacher详解
之前的字符串题解中对Manacher的思想进行了简略的介绍,在这篇文章中,我将会详细的将这个算法的初衷和具体实现理论进行解释.声明一点,这是我个人的理解,可能有不全面之处,望多包涵.在之前的几篇文章中 ...
- Opengl4.5 中文手册—C
索引 A B C D E F G H I J K L M N O P Q ...
- Java基础——集合源码解析 List List 接口
今天我们来学习集合的第一大体系 List. List 是一个接口,定义了一组元素是有序的.可重复的集合. List 继承自 Collection,较之 Collection,List 还添加了以下操作 ...
- 如何解决Python.h:No such file or directory
安装python2.7对应的dev sudo apt-get install python-dev 安装python3.6对应的dev sudo apt-get install python3-dev
- mac pycharm 里table键设置为4个空格键
Operation flow: File--Default Settings editor--code style--python
- 开源纯C#工控网关+组态软件
一. 前言 在园子潜水也七八年了.说来惭愧,这么多年虽然一直自称.NET铁杆粉丝,然仅限于回几个不痛不痒的贴,既没有发布过代码,也没有写过文章. 看着.NET和C#在国外风生水起,国内却日趋没落, ...