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:

  1. Input: candies = [1,1,2,2,3,3]
  2. Output: 3
  3. Explanation:
  4. There are three different kinds of candies (1, 2 and 3), and two candies for each kind.
  5. Optimal distribution: The sister has candies [1,2,3] and the brother has candies [1,2,3], too.
  6. The sister has three different kinds of candies.

Example 2:

  1. Input: candies = [1,1,2,3]
  2. Output: 2
  3. Explanation: For example, the sister has candies [2,3] and the brother has candies [1,1].
  4. 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

关键点:分析可能性

  1. public class Solution
  2. {
  3. public int distributeCandies(int[] candies)
  4. {
  5. HashSet<Integer> set = new HashSet<>();
  6.  
  7. for(int i=0; i<candies.length; i++)
  8. set.add(candies[i]);
  9.  
  10. // if candy number for sister >= the number of kinds for candies -> kinds
  11. // if candy number for sister < the number of kinds for candies -> candy number
  12. return Math.min(set.size(), candies.length/2);
  13. }
  14. }

参考资料: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. mshadow的原理--MXNet

    mshadow的原理--MXNet 这文章主要解释了表达式模板的工作原理(也是mshadow的主要原理),文章的前半部分是翻译自exp-template/README.md.我们会解释它为什么会影响编 ...

  2. 如何在CentOS上安装一个2048小游戏

    如何在centos上安装一个2048小游戏 最近在学习CentOS系统,就琢磨着玩点什么,然后我看到有人在玩2048小游戏,所有我就在想,为啥不装一个2048小游戏搞一下嘞,于是乎,我就开始工作啦 由 ...

  3. 浅谈IT技术女转战微电商初体验

    今天闲来无事,突然想翻看下之前写的技术博客,很是意外,居然那么多阅读量,于是想想做微商也有一段时间了,决定写写初入微商的初体验. 先自我介绍一下,本人是一名理工女,做IT行业的,这个行业也许有人了解, ...

  4. 谈一谈最近学了一段时间的node.js

    官方说明 1.NodeJS宣称其目标是“旨在提供一种简单的构建可伸缩网络程序的方法”. 2.node是一个基于Chrome V8引擎进行代码解释的.轻量.可伸缩的具有事件驱动和非阻塞I/O机制的js运 ...

  5. Jmeter运行结果分析

    1.聚合报告 以上就是我们所关心的结果 Label:页面或请求名称 Samples:运行的线程数(也可理解为请求数) Average:平均响应时间 Median:响应时间的中值 90% Line:90 ...

  6. angular $observe() 和$watch的区别

    1.$observe()是属性attributes的方法,只能在DOM属性的值发生变化时用,并且只用于directive内. 当需要监听一个包含变量的属性值时attr1="Name:{{na ...

  7. BGP协议

    BGP属于自治系统间路由协议.BGP的主要目标是为处于不同AS中的路由器之间进行路由信息通信提供保障.BGP既不是纯粹的矢量距离协议,也不是纯粹的链路状态协议,通常被称为通路向量路由协议.这是因为BG ...

  8. PHP常用字符串处理函数

    (1)strlen(string) 返回字符串长度 (2)strpos(string,find,begin) 返回find字符串第一次出现的位置(从0开始) string:处理的字符串 find:想找 ...

  9. 如何创建一个Django项目

    Django 软件框架 软件框架是由其中的各个模块组成,每个模块负责特定的功能,模块与模块之间相互协作来完成软件开发. MVC简介 MVC框架的核心思想是:解耦,让不同的代码块之间降低耦合,增强代码的 ...

  10. c# 【MVC】WebApi返回各种类型(图片/json数据/字符串)

    using System.IO; /// <summary> /// WebApi返回图片 /// </summary> public HttpResponseMessage ...