575. Distribute Candies
来源:互联网 发布:网络黄金egd交易平台 编辑:程序博客网 时间:2024/06/13 21:14
575. Distribute Candies
Given an integer array with even length, where different numbers in this array represent differentkinds of candies. Each number means one candy of the corresponding kind. You need to distribute these candiesequally in number to brother and sister. Return the maximum number ofkinds 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].
这道题给出一个偶数长度的数列,其中的数组长度代表糖果数,其中的数代表不同种类的糖果,要求把糖果分给弟弟妹妹,求妹妹能分到最多的糖果种类有多少?这个题比较简单,对于长度是2n的数组,妹妹可以获得n个糖果,如果糖果种类m小于n ,则其必然能获得m种糖果。如果m大于等于n,则其最多是在m种糖果里选择n种获得。所以妹妹最多可以获得min(m,n)种糖果。
class Solution {public:int distributeCandies(vector<int>& candies) {map<int, int> ma;int n = candies.size();if (n == 0) return 0;for (int i = 0; i < n; i++) {ma[candies[i]] = 1;}int m = ma.size();return min(m, n / 2);}};
阅读全文
0 0
- 575. Distribute Candies
- 575. Distribute Candies
- LeetCode | 575. Distribute Candies
- 575. Distribute Candies
- 575. Distribute Candies
- leetcode 575. Distribute Candies
- 575. Distribute Candies
- [LeetCode]575. Distribute Candies
- [leetcode]: 575. Distribute Candies
- 575. Distribute Candies
- 575. Distribute Candies
- [leetcode]575. Distribute Candies
- [easy]575. Distribute Candies
- LeetCode 575. Distribute Candies
- leetcode.575. Distribute Candies
- LeetCode 575. Distribute Candies
- 575. Distribute Candies
- 575. Distribute Candies
- Java面试宝典
- react-navigation跨tab路由处理
- Cube Stacking
- 爱测未来性能-性能测试之Kafka
- mysql 两种引擎下插入表数据的加锁类型
- 575. Distribute Candies
- java下length length() size()区别
- tf.argmax()以及axis解析
- IntelliJ IDEA 2017 完美注册方法(附idea for Mac破解方法)
- Java集合框架面试问题集锦
- java单例模式
- Android原生、H5、React Native(总结连载)
- 【动软.Net代码生成器】连接MySQL生成C#的POCO实体类(Model)
- crontab定时执行php脚本备份数据库