lintcode/leetcode由易至难第7题:Distribute Candies
来源:互联网 发布:c语言向上取整函数 编辑:程序博客网 时间:2024/06/05 14:58
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.Code1:
public class Solution { public int distributeCandies(int[] candies) { int a = candies.length; Arrays.sort(candies); int count = 1; for(int i = 0;i < (a-1); i++){ if(candies[i] != candies[i+1])count++; } return Math.min(count , a/2); }}
Code2:
public class Solution { public int distributeCandies(int[] candies) { Set<Integer> cat = new HashSet<>();//尝试使用HashSet for(int candy : candies){ cat.add(candy); } return cat.size() >= candies.length/2 ? candies.length/2 : cat.size(); //新接触的三目运算符 }}
阅读全文
0 0
- lintcode/leetcode由易至难第7题:Distribute Candies
- [leetCode刷题笔记]575. Distribute Candies
- LeetCode | 575. Distribute Candies
- LeetCode之Distribute Candies
- leetcode 575. Distribute Candies
- [LeetCode]575. Distribute Candies
- LeetCode 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
- 1035: 分段函数求值
- java配置文件properties
- 实名制的优越性
- iOS面试题(2.类变量的@protected,@private,@public,@package声明各有什么含义)拓展:常用框架和第三方框架
- python数据分析实践(一)
- lintcode/leetcode由易至难第7题:Distribute Candies
- oracle告警sysaux表空间满告警
- 11_Boost
- Git-常用命令&快捷命令&团队合作
- Java 可变参数
- nginx自定义502页面
- git入门级--在github创建项目
- git入门级--在github创建项目
- 自定义cell的间距