原题链接在这里:https://leetcode.com/problems/distribute-candies/#/description

题目:

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

题解:

如果糖果中distinct 的标号个数 大于等于 candies数量的一半,那姐姐有办法能拿到糖果的标号都不同。否则就只能拿到distinct标号那么多的不同糖果.

Time Complexity: O(candies.length).

Space: O(candies.length.)

AC Java:

 public class Solution {
public int distributeCandies(int[] candies) {
if(candies == null || candies.length == 0){
return 0;
} HashSet<Integer> hs = new HashSet<Integer>();
for(int i : candies){
hs.add(i);
} return hs.size()>=candies.length/2 ? candies.length/2 : hs.size();
}
}

LeetCode Distribute Candies的更多相关文章

  1. [LeetCode] Distribute Candies 分糖果

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

  2. LeetCode 1103. Distribute Candies to People

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

  3. 【Leetcode_easy】1103. Distribute Candies to People

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

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

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

  5. 【leetcode】575. Distribute Candies

    原题 Given an integer array with even length, where different numbers in this array represent differen ...

  6. 【LeetCode】575. Distribute Candies 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  7. leetcode算法:Distribute Candies

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

  8. LeetCode算法题-Distribute Candies(Java实现)

    这是悦乐书的第266次更新,第279篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第133题(顺位题号是575).给定具有偶数长度的整数数组,其中该数组中的不同数字表示不 ...

  9. LeetCode 575 Distribute Candies 解题报告

    题目要求 Given an integer array with even length, where different numbers in this array represent differ ...

随机推荐

  1. Django~1

    一 什么是web框架? 框架,即framework,特指为解决一个开放性问题而设计的具有一定约束性的支撑结构,使用框架可以帮你快速开发特定的系统,简单地说,就是你用别人搭建好的舞台来做表演. 对于所有 ...

  2. centos 6.5 设置屏幕保护

    设置屏幕保护:System -> Preferences -> Screensaver.如果需要取消屏幕保护的锁定功能,将Lock screen when screensaver is a ...

  3. PHP日期与时间戳转换

    设置时区 在php.ini中找到data.timezone去掉它前面的;号,然后设置data.timezone = "Asia/Shanghai"; 或者 ini_set('dat ...

  4. OC自动释放池autoreleasepool介绍

    自动释放池的机制是:它使得应用在创建新对象时,系统能够有效地管理应用所使用的内存. @autoreleasepool { statements } 在创建新对象时,并且系统未启动ARC特性,那么在使用 ...

  5. windows通过ftp下载linux文件

    # windows 下载 linux的文件>> ftp <domain_or_ip>>> <input_username>>> <in ...

  6. Linux Shell基础 通配符

    通配符 在 Bash 中,如果需要模糊匹配文件名或目录名,就要用到通配符.下面为常用的通配符. 表 1 通配符 通配符 作 用 ? 匹配一个任意字符 * 匹配 0 个或任意多个任意字符,也就是可以匹配 ...

  7. linux性能调分析及调优

    转:https://blog.csdn.net/luokehua789789/article/details/53007456 Linux 性能分析以及调优介绍 写在前面:计算机要解决的基本问题之一是 ...

  8. python中偏函数

    当一个函数有很多参数时,调用者就需要提供多个参数.如果减少参数个数,就可以简化调用者的负担. 比如,int()函数可以把字符串转换为整数,当仅传入字符串时,int()函数默认按十进制转换: >& ...

  9. python的计算保留小数

    1.要使得算术运算的结果有小数,则运算的对象至少有一个是float型的. 2.控制小数的位数:字符串格式化 格式:需要进行格式化的字符串%插入对象 需要进行格式化的字符串中带有一个或多个嵌入的转换目标 ...

  10. INSPIRED启示录 读书笔记 - 第24章 平滑部署

    避免更新产品导致用户反感 毫无征兆地更新不必要的版本会令用户产生反感.不是所有用户都喜欢新版本的产品.用户产生反感主要有几个原因 1.事前没有收到更新通知,用户觉得措手不及 2.用户没时间学习.适应新 ...