[LeetCode]575. Distribute Candies(妹妹最多能得到多少种糖果)
2017-05-07 20:28
399 查看
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:
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].
题目大意:
给一个长度为偶数的整数数组,数组中不同数字都代表不同糖果,将糖果平均分给弟弟和妹妹,妹妹最多能得到几种糖果。
思路:
记录糖果种类,若糖果种类大于数组的一半,妹妹最多得到candies.size()/2种糖果,否则每种糖果都可以得到
代码1:
用hash表。
定义unordered_map《int, int> HASH,定义一个整数res存储妹妹能得到糖果的种类
遍历数组,将每个数字存到对应位置中,如果存入后这个数字对应位置的数(该种糖果数量)等于1且res小于数组长度的一半,res++,最后返回res
#include <vector> #include <unordered_map> int distributeCandies(vector<int>& candies) { if(candies.size()%2 != 0) return 0; unordered_map<int, int> HASH; int res = 0; for(int c: candies){ HASH[c]++; if(HASH[c]==1 && res<candies.size()/2) res++; } return res; }
代码2:
排序后遍历
若是记录糖果种类,不用额外的内存占用,只需要定义一个整数res
#include <iostream> #include <vector> #include <assert.h> #include <algorithm> using namespace std; class Solution { public: int distributeCandies1(vector<int>& candies) { assert(candies.size()%2 == 0);//assert宏的原型定义在<assert.h>中,其作用是如果它的条件返回错误,则终止程序执行 sort(begin(candies), end(candies));//将数组排序 int ans = 0;//记录糖果种类 for (int i=0; i<candies.size(); ++i) { if(i==0 || candies[i]!=candies[i-1]) ++ans; } return min(ans, (int)candies.size()/2); } }; int main() { Solution a; int candies[] = {1, 1, 1, 2, 2, 2, 3, 4}; int candiesLength = sizeof(candies)/sizeof(candies[0]); vector<int> Candies(candies, candies+candiesLength); cout << "the maximum number of kinds of candies the sister could gain is "; cout << a.distributeCandies(Candies) << endl; return 0; }
相关文章推荐
- leetcode 575. Distribute Candies 平均糖果的最多的种类 + 直接分析
- 【LeetCode141-150】链表环,先序后序遍历(迭代非递归),LRU内存,一条直线上最多多少点,stack经典题
- 有一个糖果店的糖果有三种包装,分别是小包有6颗,中包有9颗,大包有20颗,如果我们只按整包买糖果,请问不能买到的糖果数最多是多少颗?
- 每天一道LeetCode-----平面上n个点,计算最多有多少个点在一条直线上
- LeetCode | 149. Max Points on a Line求多个点里面在一条直线上的点最多有多少个难题
- Leetcode:485. Max Consecutive Ones 找二进制串连续1最多是多少
- [leetcode] Max Points on a Line 判断最多有多少个点在同一条直线上
- 客户端如果不在域内,最多可以记住多少用户?
- 找出数组中出现次数最多的前k个元素[leetcode题]
- JVM最多支持多少个线程?
- SQL Server 最多可以有多少张表?
- LeetCode | Candy(分糖果问题)
- n级阶梯,每次走一步或两步,问最多有多少种走法
- 百度笔试题:绳子最多覆盖多少个点
- 【经典】有K张折扣券和m元最多能买多少物品(折前价ai,折后价bi)
- TextView 在布局中设置最多显示多少,后面的以...表示
- 有一个大西瓜,用水果刀平整地切,总共切9刀,最多能切成多少份,最少能切成多少份?
- 平面上画n条直线,最多能将平面分成多少部分?
- .每瓶汽水1元,两个空瓶可以置换一瓶汽水,现在有20元,最多能喝多少瓶汽水
- Malloc最多一次能分配多少内存