原题链接在这里: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. HTML学习笔记(下)

    表格标签 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3 ...

  2. scp 和 pscp

    今天在做项目遇到了两个问题,做一些总结. 1.在项目中,防火墙的的相关配置是关于cisco企业级防火墙的配置,并不是window防火墙. 2. 在Linux上,   scp     linux文件   ...

  3. MySQL备份账号权限

    grant select,show view,lock tables,trigger on confluence.* to 'DBbackup'@'127.0.0.1' identified by ' ...

  4. pd.read_csv的header用法

    默认Header = 0: In [3]: import pandas as pd In [4]: t_user = pd.read_csv(r'C:\Users\Song\Desktop\jdd_d ...

  5. maven创建web工程Spring配置文件找不到

    使用maven创建web工程,将Spring配置文件applicationContext.xml放在src/resource下,用eclipse编译时提示class path resource [ap ...

  6. web容器调用Filter和Servlet顺序学习

    web容器调用Filter和Servlet顺序学习    一直对Filter和Servlet在哪里被web容器调用迷惑,后查看tomcat源码,揭开了其面纱.1. 下面是一个简单的时序图: 2. 对上 ...

  7. javascript 时间日期处理相加相减

    var d = new Date("2008/04/15"); d.setMonth(d.getMonth() + 1 + 1);//加一个月,同理,可以加一天:getDate() ...

  8. 0.00-050613_ZC_Chapter4_20151230

    1. 32位 保护模式 段选择符 --> 段描述符(段描述符表) --> 段基地址 + 偏移量  ==> 线性地址(ZC: 这个地址就是段的开始地址) 1.2. 段限长字段LIMIT ...

  9. Eclipse下使用Maven建立简单Springboot程序

    1.创建Maven工程 2.编写pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&q ...

  10. SQL Server 2016 —— 聚集列存储索引的功能增强

    作者 Jonathan Allen,译者         邵思华         发布于     2015年6月14日   聚集列存储索引(CC Index)是SQL Server 2014中两大最引 ...