题目:

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

代码:

 class Solution {
public:
int distributeCandies(vector<int>& candies) {
stack<int> tem;
sort(candies.begin(), candies.end());
tem.push(candies[]);
for (auto c : candies) {
if ( c != tem.top())
tem.push(c);
}
if ( tem.size() > candies.size()/)
return candies.size()/;
else
return tem.size();
}
};

运行时间:276ms

LeetCode: 575 Distribute Candies(easy)的更多相关文章

  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 1103. Distribute Candies to People

    1103. Distribute Candies to People(分糖果||) 链接:https://leetcode-cn.com/problems/distribute-candies-to- ...

  4. 【leetcode】575. Distribute Candies

    原题 Given an integer array with even length, where different numbers in this array represent differen ...

  5. 【LeetCode】575. Distribute Candies 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  6. [LeetCode&Python] Problem 575. Distribute Candies

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

  7. 575. Distribute Candies

    https://leetcode.com/problems/distribute-candies/description/ 题目比较长,总结起来很简单:有个整型数组,长度是偶数,把它分成两份,要求有一 ...

  8. 575. Distribute Candies 平均分糖果,但要求种类最多

    [抄题]: Given an integer array with even length, where different numbers in this array represent diffe ...

  9. LeetCode 1103. Distribute Candies to People (分糖果 II)

    题目标签:Math 题目让我们分发糖果,分的糖果从1 开始依次增加,直到分完. for loop可以计数糖果的数量,直到糖果发完.但是还是要遍历array 给people 发糖,这里要用到 index ...

随机推荐

  1. Android下强制显示ActionBar的overflowbutton

    因为手机硬件情况的不同,在没有物理Menu键的手机上.ActionBar的overflowbutton会有显示不出来的情况,能够通过反射的方式改动ViewConfiguration类中的sHasPer ...

  2. Continuous Integration with Selenium

    I have seen a lot of queries from people who basically want to know how to blend Selenium, Maven, an ...

  3. Android判断TextView是否超出加省略号

    我们都知道通过指定android:ellipsize="end" android:singleLine="true" 可以让TextView自动截断超出部分并且 ...

  4. SAM4E单片机之旅——8、UART初步

    通信还是比让LED灯闪烁实用得多的. 这次试试使用UART,实现开发版和PC间的通信.功能比较简单,就是把PC发向开发版的内容发送回去.这次主要介绍一下UART的配置,至于通信,则使用较为简单的不断查 ...

  5. EasyPusher安卓Android手机直播推送之MediaCodec 硬编码H264格式

    本文转自Holo的博客:http://blog.csdn.net/u013758734/article/details/50834770 最近在研究EasyDarwin的Push库EasyPusher ...

  6. Theseven relationsarein threecategories:equivalent, congruent, andsimilar.

    http://www.math.pitt.edu/~xfc/math2370/chap5.pdf

  7. Qtree3

    题目描述 给出N个点的一棵树(N-1条边),节点有白有黑,初始全为白 有两种操作: 0 i : 改变某点的颜色(原来是黑的变白,原来是白的变黑) 1 v : 询问1到v的路径上的第一个黑点,若无,输出 ...

  8. centOS7安装docker步骤

    首先准备一台linux系统, Docker需要一个64位系统的系统,内核的版本必须大于3.10,可以用命令来检查是否满足要求: 满足条件后,下面开始正式安装步骤: 1.更新yum: sudo yum ...

  9. SpringBoot-(3)-RestController接口参数

    一,无参接口: //无参接口 @RequestMapping("/appSecret") public String secret() { return "EK125EK ...

  10. LLVM的总结

    LLVM 写在前面的话:无意中看到的LLVM的作者Chris Lattner相关的介绍和故事,觉得很有意思就贴上来,如果不感兴趣,可以直接跳入下一章. 关于LLVM 如果你对LLVM的由来陌生,那么我 ...