[LeetCode]575. Distribute Candies
来源:互联网 发布:淘宝网信用卡网银支付 编辑:程序博客网 时间:2024/06/05 08:39
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: 3Explanation: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: 2Explanation: 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].
public class Solution { public int distributeCandies(int[] candies) { int res=0; int girlGet = candies.length/2; HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); for(int candy: candies){ if(map.containsKey(candy)) map.put(candy, map.get(candy)+1); else map.put(candy,1); } if(map.size()>girlGet) res=girlGet; else res=map.size(); return res; }}
HashMap
阅读全文
0 0
- LeetCode | 575. Distribute Candies
- leetcode 575. Distribute Candies
- [LeetCode]575. Distribute Candies
- [leetcode]: 575. Distribute Candies
- [leetcode]575. Distribute Candies
- LeetCode 575. Distribute Candies
- leetcode.575. Distribute Candies
- LeetCode 575. Distribute Candies
- [LeetCode]575.Distribute Candies
- leetcode 575. Distribute Candies
- LeetCode: 575. Distribute Candies
- LeetCode#575. Distribute Candies
- LeetCode 575. Distribute Candies
- 【LeetCode】575. Distribute Candies
- [LeetCode]575. Distribute Candies
- [LeetCode]575. Distribute Candies
- <LeetCode>575. Distribute Candies
- leetcode 575. Distribute Candies
- 分组背包
- Linux命令更新系统时间,更新所有文件的时间
- MAVEN项目中添加jar包httpclient-4.3.5.jar,commons-logging-1.1.1.jar,httpcore-4.3.2.jar
- 表单提交方法
- 使用Spring MVC中的Interceptor和slf4j中的MDC实现日志归类标记
- [LeetCode]575. Distribute Candies
- jsf
- 【Redis基础】五种数据类型
- 猴子吃桃问题-JavaScript实现
- 关于MySQL--查询今天以及昨天、上个月等等
- 判断图片是否加载完毕
- 自定义listview holder 和adapter
- 公务员面试综合分析真题解析
- 奇数判断