题目描述:

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].

要完成的函数:

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种。

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

    int distributeCandies(vector<int>& candies)
{
int total=candies.size()/;
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)。

代码如下:

    int distributeCandies(vector<int>& candies)
{
int total=candies.size()/;
sort(candies.begin(),candies.end());
int kind=;
for(int i=;i<candies.size()-;i++)
{
if(candies[i+]!=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中有多少种数字时,可以先做一个快速排序,接着单重循环。

leetcode-575-Distribute Candies(计算一个数组中元素的种类的快速方法)的更多相关文章

  1. LeetCode 575. Distribute Candies (发糖果)

    Given an integer array with even length, where different numbers in this array represent different k ...

  2. LeetCode 575 Distribute Candies 解题报告

    题目要求 Given an integer array with even length, where different numbers in this array represent differ ...

  3. LeetCode: 575 Distribute Candies(easy)

    题目: Given an integer array with even length, where different numbers in this array represent differe ...

  4. javascript中获取字符串或数组中元素的索引

    有些时候,我们需要知道一个字符串中字符的位置,或者一个数组中元素的位置,这是就需要对该变量进行迭代操作. 对于数组,有两个方法indexOf和findIndex() , 需要注意的是,findInde ...

  5. 【leetcode】Merge Sorted Array(合并两个有序数组到其中一个数组中)

    题目: Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assum ...

  6. [LeetCode] 624. Maximum Distance in Arrays 数组中的最大距离

    Given m arrays, and each array is sorted in ascending order. Now you can pick up two integers from t ...

  7. C#实现如何判断一个数组中是否有重复的元素

    如何判断一个数组中是否有重复的元素 实现判断数组中是否包含有重复的元素方法 这里用C#代码给出实例 方法一:可以新建一个hashtable利用hashtable的Contains方法进行查找 /// ...

  8. C#实现如何判断一个数组中是否有重复的元素 返回一个数组升序排列后的位置信息--C#程序举例 求生欲很强的数据库 别跟我谈EF抵抗并发,敢问你到底会不会用EntityFramework

    C#实现如何判断一个数组中是否有重复的元素   如何判断一个数组中是否有重复的元素 实现判断数组中是否包含有重复的元素方法 这里用C#代码给出实例 方法一:可以新建一个hashtable利用hasht ...

  9. JAVA经典题--计算一个字符串中每个字符出现的次数

    需求:  计算一个字符串中每个字符出现的次数 思路: 通过toCharArray()拿到一个字符数组--> 遍历数组,将数组元素作为key,数值1作为value存入map容器--> 如果k ...

随机推荐

  1. 富文本编辑器和fastdfs的使用

    宜立方商城的系统架构a) 功能介绍(项目架构,有哪些功能模块,这些功能模块如何实现?)b) 架构讲解工程搭建-后台工程c) 使用maven搭建工程(后台工程如何搭建?)d) 使用maven的tomca ...

  2. Hibernate入门级实例

    一.开发环境 Win8 + jdk1.7 + MyEclipse + Tomcat5.0 + MySQL 说明:其实Hibernate是非常独立的框架,根本不需要MyEclipse,Eclipse,T ...

  3. Sqlserver时间函数用法(二)

    --1. 当前系统日期.时间 select getdate() --2015-01-06 09:27:27.277 --2.时间操作 dateadd 在向指定日期加上一段时间的基础上,返回新的 dat ...

  4. c语言标准输入和scanf的关系

    int a scanf("%d",&a); 什么意思,是从键盘读取一个数字存放到a中.错,scanf和所有从键盘获取输入数据的函数都不是直接从键盘获取数据的,而是从“标准输 ...

  5. js常用utils

    var utils = { /** * 日期格式化 * * @param {Date} date 指定日期 * @param {String} format * @returns {String} * ...

  6. Python创建单例模式的5种常用方法-乾颐堂

    所谓单例,是指一个类的实例从始至终只能被创建一次. 方法1 如果想使得某个类从始至终最多只有一个实例,使用__new__方法会很简单.Python中类是通过__new__来创建实例的: 1 2 3 4 ...

  7. layer使用注意事项

    ajax一定要设置为异步

  8. 多校训练4——Hehe

    递推题: dp[i]表示字符串第i个字母前有多少种不同的方法 1.出现一个hehe:dp[i]=dp[i-4]+dp[i-2] 意思是dp[i]=当前的hehe换成wqnmlgb+当前的hehe不换成 ...

  9. Smarty的条件判断语句

    (1)基本句式{if $name eq "Fred"}Welcome Sir.{elseif $name eq "Wilma"}Welcome Ma'am.{e ...

  10. Redis 数据库学习

    安装(mac) 使用homebrew安装,命令是:brew install redis. 安装完成后启动命令:brew services start redis. 使用命令redis-cli进入red ...