leetcode 575. Distribute Candies
来源:互联网 发布:淘宝装饰店铺教程 编辑:程序博客网 时间:2024/05/22 19:05
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].
import java.util.HashMap;import java.util.Map;public class Distribute_Candies_575 {public int distributeCandies(int[] candies) {Map<Integer, Integer> map=new HashMap<Integer, Integer>();int length=candies.length;int halfLength=length/2;int count=0;for(int i=0;i<candies.length;i++){int theCandy=candies[i];if(map.get(theCandy)==null){count++;map.put(theCandy, 0);}}if(count>halfLength){return halfLength;}else{return count;}}public static void main(String[] args) {// TODO Auto-generated method stubDistribute_Candies_575 d=new Distribute_Candies_575();int[] a=new int[]{1,1,2,3};System.out.println(d.distributeCandies(a));}}不过这题看到大神的解答,貌似更适合用hashset??
public int distributeCandies(int[] candies) { Set<Integer> set = new HashSet<>(); for(Integer candie : candies) { set.add(candie); if(set.size() == candies.length/2) return set.size(); } return Math.min(set.size(), candies.length/2);}HashSet类,是存在于java.util包中的类。同时也被称为集合,该容器中只能存储不重复的对象。
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
- 浅析C语言之uint8_t / uint16_t / uint32_t /uint64_t
- sqlserver还原差异备份
- centos7搭建DHCP简要服务器
- Java实现八大排序算法
- java__for找出数组中最大,最小值
- leetcode 575. Distribute Candies
- [RK3288][Android6.0] AT24C02驱动分析及功能增加小结
- Mybatis源码分析之插件(plugins)源码详解
- grails 中返回 json
- 火柴棍
- Codeforces 807E Prairie Partition 贪心思维+二分
- 实现数据源为List<ImageView>的简单的ViewPage效果
- Java集合框架(3)——HashMap
- oracle 查询取第二行值 rank 与 ROWNUM