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

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

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

JAVA CODE

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

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. python 魔法方法(学习过程的笔记)

    有小伙伴会问,什么是python的魔法方法,python的魔法方法有什么用呢, 它们在面向对象的Python的处处皆是.它们是一些可以让你对类添加"魔法"的特殊方法. 它们经常是两 ...

  2. sql优化策略之索引失效情况二

    详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp63   接第一篇索引失效分析:http://grefr.iteye.co ...

  3. JOptionPane弹框常用实例

    最近在做swing程序中遇到使用消息提示框的,JOptionPane类其中封装了很多的方法. 很方便的,于是就简单的整理了一下. 1.1 showMessageDialog 显示一个带有OK 按钮的模 ...

  4. CCIE-MPLS VPN-实验手册(上卷)

    看完了看完了看完了,豪爽豪爽豪爽,一个月了,写得挺棒.总共14个mpls vpn的实验,为留下学习的痕迹,原封不动献出. CCIE实验手册 (路由部分-MPLSVPN基础篇) [CCIE]  JUST ...

  5. 团队作业4——第一次项目冲刺(Alpha版本)2017.4.22

    昨天来不及编写,这是4月22日的日志,现在补上. 1.开完站立式会议后的合照 2.任务分解图 3.开会讨论的结果,任务分派 队员 今日进展 明日安排 陈鑫龙 原型设计图分析,设计登陆界面原稿 实现登陆 ...

  6. 201521123053《Java程序设计》第3周学习总结

    ---恢复内容开始--- 1. 本周学习总结 2. 书面作业 1. 代码阅读 以上代码可否编译通过?哪里会出错?为什么?尝试改正?        如果创建3个Test1对象,有内存中有几个i,几个j? ...

  7. 201521123038 《Java程序设计》 第二周学习总结

    201521123038 <Java程序设计> 第二周学习总结 1.本章学习总结 学会在Java程序中使用函数,使程序层次更清晰 使用StringBuilder编写代码,减少内存空间的占用 ...

  8. python只re模块

    while True: phone_number=input('please input your phone number:') if len(phone_number)==11 \ and pho ...

  9. 浅谈SQL优化入门:2、等值连接和EXPLAIN(MySQL)

    1.等值连接:显性连接和隐性连接 在<MySQL必知必会>中对于等值连接有提到两种方式,第一种是直接在WHERE子句中规定如何关联即可,那么第二种则是使用INNER JOIN关键字.如下例 ...

  10. 反转字符串的几种实现(Java)

    反转字符串的几种实现(Java) 首先第一种是利用Java中的类库对象进行反转 //第一种 使用Java类库的diam实现反转 public String reverse(String str){ S ...