前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >leetcode-575-Distribute Candies(计算一个数组中元素的种类的快速方法)

leetcode-575-Distribute Candies(计算一个数组中元素的种类的快速方法)

作者头像
chenjx85
发布2018-05-22 16:28:57
5170
发布2018-05-22 16:28:57
举报

题目描述:

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:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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].

要完成的函数:

int distributeCandies(vector<int>& candies) 

说明:

1、这道题给了一个vector,里面每个数字代表一种糖果,比如[1,1,2,2,3,3],代表1糖果有2个,2糖果有2个,3糖果有2个。这个vector的长度必定为偶数,要把糖果均分给哥哥和妹妹,妹妹能分到的一半糖果最多能有多少种。

2、假如我们知道有n种糖果,妹妹能分到m个糖果,如果n<=m,那说明里面重复的很多,比如[1,1,1,1,2,2],妹妹能分到3个糖果,而糖果只有2种,那么妹妹最多能得到的种类数也不会超过n,只能多拿一些重复的了。

如果n>m,也就是说糖果种类比妹妹能拿到的糖果个数还多,那说明有很多种类各异的,比如[1,2,3,4,5,5],妹妹能分到3个糖果,而糖果有5种,那么妹妹能得到的最多种类数也只有3种。

思路清晰,我们可以构造如下代码:

代码语言:javascript
复制
    int distributeCandies(vector<int>& candies) 
    {
        int total=candies.size()/2;
        set<int>s1(candies.begin(),candies.end());
        int kind=s1.size();
        if(kind<=total)
            return kind;
        else
            return total;
    }

这是最简单的实现方法,利用set得到了种类数。

上述代码实测333 ms,beats 30.13% of cpp submissions。

3、改进:

我们使用set,其实是把vector中的元素一个个加进去,每碰到一个元素就判断这个元素有没有出现过,如果有就不加入,如果没有就加入。判断的这个过程其实又是一个循环。

所以set的办法其实是双重循环,O(n^2)。

但我们其实并不需要处理数,我们所需要的,只是知道vector中有多少种数。

所以我们其实可以对vector做一个快速排序,然后做单重循环,如果前一个数和后一个数不一样,那么种类数+1。

这样子的排序+单重循环的方法,时间复杂度低于O(n^2)。

代码如下:

代码语言:javascript
复制
    int distributeCandies(vector<int>& candies) 
    {
        int total=candies.size()/2;
        sort(candies.begin(),candies.end());
        int kind=1;
        for(int i=0;i<candies.size()-1;i++)
        {
            if(candies[i+1]!=candies[i])
                kind++;
        }
        if(kind<=total)
            return kind;
        else
            return total;
    }

上述代码实测258ms,beats 82.50% of cpp submissions。

4、另一种方法:

因为题目限定了数的范围在[-100,000,100,000],所以其实我们可以开辟一个长度为200001的vector。

接着迭代给定vector,更新长度为200001的vector的值。

最后再迭代这个长vector,看一下有多少种。

但是由于长vector长度太长了,所以这种方法花费时间很多,不是很推荐。这里只是做一个扩展介绍。

这道题的启示还是:当碰到需要判断vector中有多少种数字时,可以先做一个快速排序,接着单重循环。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-05-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目描述:
  • 要完成的函数:
  • 说明:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档