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

分析:数字编号的不同代表糖果种类的不同,将一堆糖果平均分给俩兄妹,妹妹的种类要最多。

思路:只存在两种情况:种类数大于等于总数的一半时,妹妹会有一半种类数的糖果;种类数小于总数的一半时,妹妹会拿到所有种类数的糖果。

JAVA CODE

  1. class Solution {
  2. public int distributeCandies(int[] candies) {
  3. // 需要将整型数组转成interger,因为基本类型不能直接转成set。
  4. Integer[] cc = new Integer[candies.length];
  5. for (int i = 0; i < cc.length; i++)
  6. cc[i] = new Integer(candies[i]);
  7. return Math.min(new HashSet<Integer>(Arrays.asList(cc)).size(), candies.length/2);
  8. }
  9. }

Distribute Candies的更多相关文章

  1. 【Leetcode_easy】1103. Distribute Candies to People

    problem 1103. Distribute Candies to People solution:没看明白代码... class Solution { public: vector<int ...

  2. LeetCode 1103. Distribute Candies to People

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

  3. LeetCode 575. Distribute Candies (发糖果)

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

  4. leetcode算法:Distribute Candies

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

  5. [LeetCode] Distribute Candies 分糖果

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

  6. [Swift]LeetCode575. 分糖果 | 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 differ ...

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

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

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

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

随机推荐

  1. postman 第3节 API请求和查看响应结果(转)

    请求 postman支持很多请求类型,界面左侧可以看到请求类型:get.post.put.patch等,右侧是发送和保存按钮,下方是请求支持的认证方式.信息头.信息体.私有脚本和测试结果.下面我们介绍 ...

  2. Apple公司开发者账号申请(2017包含邓白氏码申请)

    1.首先看需要那种账号 2.这个需要的是公司开发者账号,首先我们注册一个普通apple账号 打开网址 https://developer.apple.com 进入点击Account 进入登录页面,点击 ...

  3. Junit单元测试实例

    1.非注解 public class Test { @org.junit.Test public void testPrice() { ClassPathXmlApplicationContext b ...

  4. maven 自我学习笔记

    1.常用网站: maven.apache.org http://mvnrepository.com/   2.命令 mvn -v 查看maven的版本 mvn -compile 在项目的根目录下编译项 ...

  5. shell脚本进阶之循环判断

    p.MsoNormal,li.MsoNormal,div.MsoNormal { margin: 0cm; margin-bottom: .0001pt; text-align: justify; f ...

  6. Python杨辉三角形

    RT Show me the Code def triangles(): b = [1] while(True): yield b b = [1] + [b[i] + b[i+1] for i in ...

  7. 四则运算 WEB

    coding.net:https://git.oschina.net/ysh0904/WEB.git 一.需求分析 记录用户的对错总数,程序退出再启动的时候,能把以前的对错数量保存并在此基础上增量计算 ...

  8. 团队作业8——第二次项目冲刺(Beta阶段)第二天

    BETA阶段冲刺第二天 1.当天站立式会议 2.每个人的工作 (1) 昨天已完成的工作: Alpha阶段的Bug修复 (2) 今天计划完成的工作: 编写前端页面 (3) 工作中遇到的困难: 对于前端页 ...

  9. 201521123073 《Java程序设计》第6周学习总结

    1. 本章学习总结 2. 书面作业 1.clone方法 1.1 Object对象中的clone方法是被protected修饰,在自定义的类中覆盖clone方法时需要注意什么? 1.2 自己设计类时,一 ...

  10. 201521123011《Java程序设计》第5周学习总结

    1. 本周学习总结 1.1 尝试使用思维导图总结有关多态与接口的知识点. 参考资料: 百度脑图 XMind 2. 书面作业 作业参考文件下载 1.代码阅读:Child压缩包内源代码 1.1 com.p ...