原题链接在这里: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. 前端之 Ajax(补)

    概述 对于WEB应用程序:用户浏览器发送请求,服务器接收并处理请求,然后返回结果,往往返回就是字符串(HTML),浏览器将字符串(HTML)渲染并显示浏览器上. 1.传统的Web应用 一个简单操作需要 ...

  2. 每天一个Linux命令(50)netstat命令

        netstat命令用来打印Linux中网络系统的状态信息,可让你得知整个Linux系统的网络情况.     (1)用法:     用法:  netstat [选项参数]     (2)功能: ...

  3. Linux权限管理 文件特殊权限

    概述 除了我们前面介绍的rwx权限外,Linux中还有另外三种特殊权限:SUID,SGID,SBIT   权限    执行条件 执行示例 SUID s出现在文件所有者的x权限上. 1. SUID只能用 ...

  4. 协程(Coroutines)实现fibonacci函数

    def fibonacci(): yield 1 yield 1 l=[1,1] while True: l=[l[-1],sum(l[-2:])] yield l[-1] def tribonacc ...

  5. 主攻ASP.NET MVC4.0之重生:Jquery Mobile 表单元素

    相关代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <tit ...

  6. java基础之final/static/static final

    一.final 1.final修饰变量(常量) final修饰的成员变量表示常量,一旦给定初值既无法改变 2.final方法 final修饰方法,表示该方法不能被子类重写 好处:比非final方法要快 ...

  7. Docker 数据收集利器:cadvisor

    gitHub地址:https://github.com/google/cadvisor cAdvisor cAdvisor (Container Advisor) provides container ...

  8. DBUtiles中的简单使用(QueryRunner和ResultSetHandler的手动实现)

    DBUtiles是一个很好的处理JDBC的工具类.(DbUtils is a small set of classes designed to make working with JDBC easie ...

  9. wampserver安装在服务器中,但是mysql不能远程登录的解决方案

    利用mysql workbench或者Navicat连接服务器的mysql时,有时候会出现拒绝访问, 因为在mysql中没有赋予其他用户权限,只能本地登录,所以要进行设置. 设置如下: 打开mysql ...

  10. java.net.UnknownHostException异常处理

    1.问题描述 最近迁移环境,在Linux系统下部署Java产品的应用,后台报出如下异常,系统报找不到名为“xxx-houtai1”的主机: 1 java.net.UnknownHostExceptio ...