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. php中获取当前系统时间、时间戳

    今天写下otime($time, $now)为将时间格式转为时间戳,$time为必填.清楚了这个,想了解更多,请继续往下看. 3. date($format)用法比如:echo date(‘Y-m-d ...

  2. java从控制台接收一个数字

    //时间:2017/7/22//作者:江骆//功能:从控制台接收一个数import java.io.*;  //引入一个IO流的包public class helloworld1{    public ...

  3. python基础之字典、赋值补充

    字典常用操作: 存/取info_dic={'name':'egon','age':18,'sex':'male'} print(info_dic['name11111111']) print(info ...

  4. lintcode.66 二叉树前序遍历

    二叉树的前序遍历    描述 笔记 数据 评测 给出一棵二叉树,返回其节点值的前序遍历. 您在真实的面试中是否遇到过这个题? Yes 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返 ...

  5. 03_Ext_Viewport_Window_Dialog

    Viewport Viewport 代表整个浏览器窗口,直接渲染到document.body节点,取代页面中的所有内容.一般作为应用程序主界面. 随着浏览器显示区域的大小自动改变,一个页面中只能有一个 ...

  6. MapReduce中Combiner规约的作用以及不能作为MR标配的原因

    作用:在Mapper端对数据进行Combine归约处理,Combine业务逻辑与Reducer端做的完全相同.处理后的数据再传送到Reducer端,再做一次归约.这样的好处是减少了网络传输的数量.在M ...

  7. JavaScript中的alert、confirm、prompt

    alert: var a=alert('Alert');//界面只有一個確定alert(a);   //返回值為undefined confirm: var c= confirm('Confirm') ...

  8. Https系列之一:https的简单介绍及SSL证书的生成

    Https系列会在下面几篇文章中分别作介绍: 一:https的简单介绍及SSL证书的生成二:https的SSL证书在服务器端的部署,基于tomcat,spring boot三:让服务器同时支持http ...

  9. 修改NSMutableArray中的元素时的注意事项

    最近做项目遇到从文件加载数组,并对数组中的元素进行操作的问题,特意写了个Demo,记录下要注意的东西: 代码如下: NSArray *array = @["]; NSMutableArray ...

  10. D - DZY Loves Hash CodeForces - 447A

    DZY has a hash table with p buckets, numbered from 0 to p - 1. He wants to insert n numbers, in the ...