LeetCode<Day2> Distribute Candies
来源:互联网 发布:linux cern 知乎 编辑:程序博客网 时间:2024/06/03 12:51
package Day2;/** * Created by syw on 2017/12/10. *//**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. * */import org.junit.Test;import java.util.HashSet;import java.util.Set;/**思路:如果糖果种类大于糖果数的二分之一,那么妹妹最多拿到糖果数的1/2,否则就是每种都拿到也就是糖果的种类数。 * */public class DistributeCandy { public static void main(String[] args) { int [] candies = {1,2,1,2,1,3}; System.out.println(distributeCandies(candies)); } public static int distributeCandies(int[] candies) { Set<Integer> set = new HashSet<Integer>(); for ( int s : candies) { set.add(s); } return set.size()>=candies.length/2 ? candies.length/2 : set.size(); }}
PS:仅仅记录自己LeetCode练习过程,如有借鉴别人答案请注明谅解。
阅读全文
0 0
- LeetCode<Day2> 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
- 【LeetCode】575. Distribute Candies
- Sqlmap初步了解
- LeetCode-023 Merge k Sorted Lists
- 走redis缓存和不走缓存TPS差异明显对比
- 状态机(三):Verilog设计注意问题
- 第二周 Linux基础命令入门(二)
- LeetCode<Day2> Distribute Candies
- the general framework of RL_DQN
- java小细节不然很容易报错或者出逻辑问题得不到自己想要的。
- ARM的商业模式和ARM各种版本号区分
- 3ds Max导出OBJ的mtl贴图路径不正确
- 根据id分表
- SVN 使用简介及个人总结
- 131. Palindrome Partitioning
- C++之inline函数