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

思路:

用一个map统计每种蜡烛的的个数,同时能够得到总共的蜡烛种类数,如果种类数很少,少于蜡烛总数一半,则返回种类数。

如果种类数很多,多余蜡烛总数一半,返回蜡烛总数一半。

int distributeCandies(vector<int>& candies)
{
int total = candies.size();
map<int,int>candyKind;
for(int i =;i<total;i++)
{
candyKind[candies[i]]++;
}
int kind = candyKind.size();
if(kind<=total/) return kind;
else return total/;
}

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

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

  5. 【leetcode】575. Distribute Candies

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

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

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

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

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

  8. 575. Distribute Candies

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

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

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

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

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

随机推荐

  1. [刷题]Codeforces 794C - Naming Company

    http://codeforces.com/contest/794/problem/C Description Oleg the client and Igor the analyst are goo ...

  2. MyEclipse修改项目名称后,部署到tomcat问题。

    1.问题描述: 修改项目名称后,部署到tomcat server,部署出来的文件夹名还是旧的名称. 2.解决方案: 光把项目重命名是不够的,还要修改一下Myeclipse里面的配置. a). 工程名- ...

  3. 密码学之DES/AES算法

    本文示例代码详见:https://github.com/52fhy/crypt-demo DES DES全称为Data Encryption Standard,即数据加密标准,是一种使用密钥加密的块算 ...

  4. JEESZ-Redis分布式缓存安装和使用

    独立缓存服务器: Linux CentOS Redis 版本: 3.0下面我们针对于Redis安装做下详细的记录:编译和安装所需的包:# yum install gcc tcl创建安装目录:# mkd ...

  5. Nginx http和https 共存

    nginx https 配置,添加证书启动https server { listen default_server; listen ; server_name _; ssl on; ssl_certi ...

  6. Docker4Windows -- 从外部(非本机host)访问 由docker container运行的程序

    背景 当我们在windows 上面运行docker container的时候,我们需要借助于模拟器(例如,Virtual box/Hyper V),她的目的主要是在我们的windows系统上面模拟出一 ...

  7. GO的初始简书(一)简介安装

    已经玩了很长一段时间的golang了,做个gopher,下面我将逐步展示各种go语言的开发,从入门开始哦,完全是凭着自己学习和实践的结果展示,如果有说的不对的,请指正. 简介 go语言是由Google ...

  8. Mysql5.7忘记root密码及修改root密码的方法

    Mysql 安装成功后,输入 mysql --version 显示版本如下 mysql Ver 14.14 Distrib 5.7.13-6, for Linux (x86_64) using 6.0 ...

  9. Reinforcement Learning in R

    Reinforcement learning has gained considerable traction as it mines real experiences with the help o ...

  10. Workout Wednesday Redux (2017 Week 3)

    I had started a "52 Vis" initiative back in 2016 to encourage folks to get practice making ...