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:

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

 题目标签:HashTable
  这道题目给了我们一个糖果array, 里面的每一个数字代表了一种种类的糖。要求我们找出sister可以拿到最多种类的糖的数量。那么我们来看一下,给的糖果一定是偶数的,那么sister可以拿到的数量一定是 candies.length / 2, 糖果总数的一半。接下来有两种可能性会发生,1 - 如果sister糖果的数量 大于等于 糖果的种类数量, 那么 sister最多可以拿到的糖果种类数量就等于,糖果的种类数量;2 - 如果sister糖果的数量 小于 糖果的种类数量, 那么sister最多可以拿到的糖果种类数量 就等于 她自己的糖果的数量。
 

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 算法题目列表 - LeetCode Algorithms Questions List
 
 

最新文章

  1. 微信上传文章素材—ASP.NET MVC从View层传数据到Controller层
  2. 多个div居中显示
  3. java工程中的.classpathaaaaaaaaaaaaaaaa&lt;转载&gt;
  4. html CSS 学习总结
  5. slice、substring和substr
  6. android UI开源库
  7. SqlServer之存储过程
  8. 学习spring前,先了解了解代理模式
  9. HTML中锚点的使用
  10. [转载]Fiddler为所欲为第四篇 直播源抓取与接口分析 [四]
  11. (1)Linux常用的运维平台和工具
  12. Nginx使用教程(一):下载并编译安装Nginx
  13. yum 命令下载安装Openjdk
  14. 转:ios导航栏设置
  15. 【Log】logback指定配置文件(二)
  16. 一天半时间大致的学习了HTML和CSS.
  17. 【BZOJ】2820: YY的GCD
  18. SQL语言 之 数据查询
  19. struts2中各个jar包作用 (转)
  20. c# 引用类型对象的深拷贝

热门文章

  1. PIC单片机状态寄存器中的C(进位/借位位标志)
  2. java控制台输入输出
  3. JS控制台打印星星,总有你要的那一款~
  4. 进入css3动画世界(一)
  5. Redis介绍——Linux环境Redis安装全过程和遇到的问题及解决方案
  6. .NET十年回顾
  7. hdu4686 Arc of Dream 2013 Multi-University Training Contest 9矩阵快速幂
  8. JavaScript 版数据结构与算法(四)集合
  9. JS实现数组每次只显示5条数据
  10. MVVM -- CallMethodAction 和 InvokeCommandAction