LeetCode Distribute Candies
原题链接在这里: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:
- The length of the given array is in range [2, 10,000], and will be even.
- 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的更多相关文章
- [LeetCode] Distribute Candies 分糖果
Given an integer array with even length, where different numbers in this array represent different k ...
- LeetCode 1103. Distribute Candies to People
1103. Distribute Candies to People(分糖果||) 链接:https://leetcode-cn.com/problems/distribute-candies-to- ...
- 【Leetcode_easy】1103. Distribute Candies to People
problem 1103. Distribute Candies to People solution:没看明白代码... class Solution { public: vector<int ...
- LeetCode 575. Distribute Candies (发糖果)
Given an integer array with even length, where different numbers in this array represent different k ...
- 【leetcode】575. Distribute Candies
原题 Given an integer array with even length, where different numbers in this array represent differen ...
- 【LeetCode】575. Distribute Candies 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- leetcode算法:Distribute Candies
Given an integer array with even length, where different numbers in this array represent different k ...
- LeetCode算法题-Distribute Candies(Java实现)
这是悦乐书的第266次更新,第279篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第133题(顺位题号是575).给定具有偶数长度的整数数组,其中该数组中的不同数字表示不 ...
- LeetCode 575 Distribute Candies 解题报告
题目要求 Given an integer array with even length, where different numbers in this array represent differ ...
随机推荐
- 010_Hadoop配置测试成功后关机重启浏览器打不开dfs和MP
针对Hadoop成功配置并测试通过,第二次(关机重启)后Hadoop打不开的问题,一般都是因为防火墙的问题,将防火墙关闭后就可以了. 更细致的现象为start-all.sh启动,五大守护进程启动成功, ...
- Ubuntu16.04下编译android6.0源码
http://blog.csdn.net/cnliwy/article/details/52189349 作为一名合格的android开发人员,怎么能不会编译android源码呢!一定要来一次说编译就 ...
- python爬虫之html解析Beautifulsoup和Xpath
Beautiifulsoup Beautiful Soup 是一个HTML/XML的解析器,主要的功能也是如何解析和提取 HTML/XML 数据.BeautifulSoup 用来解析 HTML 比较简 ...
- INSPIRED启示录 读书笔记 - 第27章 合理运用瀑布式开发方法
瀑布式开发方法的基本原则 1.采用阶段式开发:软件开发过程被事先分成固定的几个阶段,撰写书面的需求说明文档.设计高层软件架构.设计低层细节.编写代码.测试.部署 2.采用阶段式评审:每个阶段结束后,对 ...
- SQLite3时间函数小结
import sqlite3 conn = sqlite3.connect('/tmp/sqlite.db') cur = conn.cursor() 接下来干嘛呢?建一张表吧.这里需要注意的是,SQ ...
- 使用kibana进行简单的CRUD和版本控制
使用: ##创建文档之前先创建索引 PUT /toov5 ##查询索引 GET /toov5 ##创建文档 /索引/类型/id PUT /toov5/user/1 { "name" ...
- Oracle数据库连接生成DataX的job-Json
package com.bbkj.main; import com.bbkj.DbUtils.ConnectionPoolManager; import com.bbkj.DbUtils.DbUtil ...
- 未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0” 提供程序
我在Web App程序里面用“Microsoft.Jet.OLEDB.4.0”来连接Excel文件,导入到数据库,在Windows 2003+ Office 2007 的环境下正常,但是在Window ...
- hadoop 指定 key value分隔符
原文:http://wingmzy.iteye.com/blog/1260570 hadoop中的map-reduce是处理<key,value>这样的键值对,故指定<key,val ...
- 在环境变量里设置VI中TAB缩进
终端上的tab键默认是缩进8个空格的(记住8个空格不等于一个tab, tab和空格不是一个概念) 一般设置vim的tab(制表符)的缩进的时候都这样:set tabstop=4 ” 表示让tab的宽度 ...