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. 微信小程序购物车产品计价

    微信小程序购物车产品计价: 问题:当选中商品,价格累加时会出现无限循环小数 解答:在计算前先parseFloat(变量),再计算的最后使用(变量).toFixed(2)保留两位小数 例如: jiaCa ...

  2. lintcode.67 二叉树中序遍历

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

  3. 鸟哥Linux学习笔记05

    1,          文件系统通常会将 权限与属性放置到inode中,至于实际数据则放置到data block块中.另外还有一个超级块(superblock)会记录整个文件系统的整体内容,包括ino ...

  4. python os语法

    前几天做了一个文件替换功能用到些python os的功能,感觉python os模块的功能非常的强大, 如果你希望你的python程序能够与平台无关的话,这个模块是尤为重要的.即它允许一个程序在编写后 ...

  5. 实验:体会Oracle权限/角色赋予的差异

    环境:Oracle 11.2.0.4 目的:验证业务用户的权限/角色赋予的差异 现在创建两个用户jingyu2和jingyu3: SYS@jyzhao1> create user jingyu2 ...

  6. JavaScript自动化构建工具入门----grunt、gulp、webpack

    蛮荒时代的程序员: 做项目的时候,会有大量的js 大量的css   需要合并压缩,大量时间需要用到合并压缩 在前端开发中会出现很多重复性无意义的劳动  自动化时代的程序员: 希望一切都可以自动完成  ...

  7. c语言中的内存浅析

    1.栈(stack):存局部变量.函数,调用函数时会开辟栈区,函数结束时就自动回收,遵循后进先出的原则,从高地址向低地址增长. 2.堆(heap):malloc.realloc.calloc等开辟的内 ...

  8. JAVA基础---编码解码

    所谓编码 即char->byte 所谓解码 即byte->char ISO-8859-1 中文字符会被黑洞吸收 全部变为"?" GB2312 汉字可以被编码为双字节 但 ...

  9. Quartz源码——scheduler.start()启动源码分析(二)

    scheduler.start()是Quartz的启动方式!下面进行分析,方便自己查看! 我都是分析的jobStore 方式为jdbc的SimpleTrigger!RAM的方式类似分析方式! Quar ...

  10. HDFS概述(2)————Block块大小设置

    以下内容转自:http://blog.csdn.net/samhacker/article/details/23089157?utm_source=tuicool&utm_medium=ref ...