LeetCode【#575】Distribute Candies

题目链接:

点击跳转

 

题目:

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:

  1. The length of the given array is in range [2, 10,000], and will be even.
  2. The number in given array is in range [-100,000, 100,000].


题目分析:

给一个偶数长度的数组,其中不同的数字表示不同糖果种类,现在要将糖果分给哥哥和妹妹。要求给妹妹分配的糖果的种类最多,输出最多种类是多少。

 

解题思路:

先统计所有的糖果种类,即数组中有多少个不同的数字,假设数组长度为 n ,最佳情况是妹妹可以分得 n/2 种不同类型的糖果。

所以统计数组中,不同数字个数,此时分成两种情况:

不同数字个数 <= n/2,那么最多妹妹可分得 "不同数字个数" 数量的糖果类型

不同数字个数 > n/2 ,那么妹妹本来是可以得到"不同数字个数" 数量的糖果类型,但是一个人最多能得到 n/2 颗糖果,所以此时妹妹得到的糖果类型数量是 n/2

因此这道题最重要的就是求出数组中有多少个不同数字,即去重问题。

首先想到了C++中的 set 用法,它set不允许两个元素有相同的键值。所以可以实现去重(同时还可以自动对 set 内的元素进行排序)。

而用时较大

Runtime: 408 ms, faster than 23.80% of C++ online submissions for Distribute Candies.

Memory Usage: 54.8 MB, less than 32.26% of C++ online submissions for Distribute Candies.

怀疑是使用 set 的原因,所以我自己弄了去重,即弄一个数组用于记相应数出现的次数,如果是第一次出现,种类数+1;否则不处理。

这个效果比用 set 的快

Runtime: 192 ms, faster than 96.91% of C++ online submissions for Distribute Candies.

Memory Usage: 17.4 MB, less than 95.16% of C++ online submissions for Distribute Candies.

相应的,Java里也有一个容器 hashSet,与C++中的 set 的区别是:都可以去重,但是 set 还可以排序,而 hashSet 不可以。

 

AC代码:

第一种:

class Solution {
public:
    int distributeCandies(vector<int>& candies) {
        
        int n = candies.size();
        set<int> iset;
        for(int i = 0;i < n;i++){
            iset.insert(candies[i]);
        }
        int res = iset.size();
        if(res > n/2)
            res = n/2;
        
        return res;
    }
};

 

第二种:

class Solution {
public:
    int distributeCandies(vector<int>& candies) {
        
        int n = candies.size();
        int res = 0;
        int num[300000];
        memset(num,0,sizeof(num));
        
        for(int i = 0;i < n;i++)
        {
            if(!num[candies[i]+100000])
                res++;
            num[candies[i]+100000]++;
        }
        
        if(res > n/2)
            res = n/2;
        return res;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值