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
 
 

LeetCode 575. Distribute Candies (发糖果)的更多相关文章

  1. LeetCode 575 Distribute Candies 解题报告

    题目要求 Given an integer array with even length, where different numbers in this array represent differ ...

  2. 575. Distribute Candies 平均分糖果,但要求种类最多

    [抄题]: Given an integer array with even length, where different numbers in this array represent diffe ...

  3. LeetCode: 575 Distribute Candies(easy)

    题目: Given an integer array with even length, where different numbers in this array represent differe ...

  4. LeetCode 1103. Distribute Candies to People

    1103. Distribute Candies to People(分糖果||) 链接:https://leetcode-cn.com/problems/distribute-candies-to- ...

  5. LeetCode 1103. Distribute Candies to People (分糖果 II)

    题目标签:Math 题目让我们分发糖果,分的糖果从1 开始依次增加,直到分完. for loop可以计数糖果的数量,直到糖果发完.但是还是要遍历array 给people 发糖,这里要用到 index ...

  6. [LeetCode] Distribute Candies 分糖果

    Given an integer array with even length, where different numbers in this array represent different k ...

  7. 【leetcode】575. Distribute Candies

    原题 Given an integer array with even length, where different numbers in this array represent differen ...

  8. 【LeetCode】575. Distribute Candies 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  9. [LeetCode&Python] Problem 575. Distribute Candies

    Given an integer array with even length, where different numbers in this array represent different k ...

随机推荐

  1. mybatis-resultMap使用与详解

    1,当数据库的字段名与属性名称不一致时,在mybatis中如何处理? 第一种方式: 采用投影对字段重命名<select id="load" parameterType=&qu ...

  2. IT连创业系列:创业者逆境下的思维

    距上篇文章,又半个多月过去了,是时候来一发阶段性的总结了. 可能最近比较懒,也可能是想不到写文的主题,故写文已变成越来越艰难的一个任务. 这个系列的大标题,也改了:它从<一个想法>到< ...

  3. oracle存储过程中is和as区别

    在存储过程(PROCEDURE)和函数(FUNCTION)中没有区别:在视图(VIEW)中只能用AS不能用IS:在游标(CURSOR)中只能用IS不能用AS.

  4. Spring 3.x 读书笔记

    第一:如果使用BeanFactory作为Spring Bean的工厂类,则所有的bean都是在第一次使用该Bean的时候实例化 第二:如果使用ApplicationContext作为Spring Be ...

  5. Java 数组扩容

    在添加数据到达数组的上限的时候数组进行扩容: public void resizeArrayCaptcity(){ if(size>=arr.length){ Emp [] arr2=new   ...

  6. mybatis typehandler

    建立TypeHandler 我们知道java有java的数据类型,数据库有数据库的数据类型,那么我们在往数据库中插入数据的时候是如何把java类型当做数据库类型插入数据库,在从数据库读取数据的时候又是 ...

  7. 化繁为简 经典的汉诺塔递归问题 in Java

    问题描述   在世界中心贝拿勒斯(在印度北部)的圣庙里,一块黄铜板上插着三根宝石针.印度教的主神梵天在创造世界的时候,在其中一根针上从下到上地穿好了由大到小的64片金片,这就是所谓的汉诺塔.不论白天黑 ...

  8. .Neter玩转Linux系列之一:初识Linux

    一.为什么要学习Linux (1)首先我们欣赏一下,曾经的微软是如何看待Linux的,是不是很惊讶,微软还是很可爱的(#^.^#) 如今的微软看待Linux的态度:有人说微软技术那么厉害,难道微软就不 ...

  9. Python 实现的随机森林

    随机森林是一个高度灵活的机器学习方法,拥有广泛的应用前景,从市场营销到医疗保健保险. 既可以用来做市场营销模拟的建模,统计客户来源,保留和流失.也可用来预测疾病的风险和病患者的易感性. 随机森林是一个 ...

  10. spring框架总结(01)

    1.spring是什么? sprint其实就是一个开源框架,是于2003年兴起的一个轻量级的java开发框架,是有Road Johnson创建的,简单的来说spring是一个分层的JavaSE/EE( ...