[leetcode-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].
思路:
用一个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]的更多相关文章
- LeetCode 575. Distribute Candies (发糖果)
Given an integer array with even length, where different numbers in this array represent different k ...
- LeetCode 575 Distribute Candies 解题报告
题目要求 Given an integer array with even length, where different numbers in this array represent differ ...
- LeetCode: 575 Distribute Candies(easy)
题目: Given an integer array with even length, where different numbers in this array represent differe ...
- LeetCode 1103. Distribute Candies to People
1103. Distribute Candies to People(分糖果||) 链接:https://leetcode-cn.com/problems/distribute-candies-to- ...
- 【leetcode】575. Distribute Candies
原题 Given an integer array with even length, where different numbers in this array represent differen ...
- 【LeetCode】575. Distribute Candies 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- [LeetCode&Python] Problem 575. Distribute Candies
Given an integer array with even length, where different numbers in this array represent different k ...
- 575. Distribute Candies
https://leetcode.com/problems/distribute-candies/description/ 题目比较长,总结起来很简单:有个整型数组,长度是偶数,把它分成两份,要求有一 ...
- 575. Distribute Candies 平均分糖果,但要求种类最多
[抄题]: Given an integer array with even length, where different numbers in this array represent diffe ...
- LeetCode 1103. Distribute Candies to People (分糖果 II)
题目标签:Math 题目让我们分发糖果,分的糖果从1 开始依次增加,直到分完. for loop可以计数糖果的数量,直到糖果发完.但是还是要遍历array 给people 发糖,这里要用到 index ...
随机推荐
- Trie树详解
1. 概述 Trie树,又称字典树,单词查找树或者前缀树,是一种用于快速检索的多叉树结构,如英文字母的字典树是一个26叉树,数字的字典树是一个10叉树.Trie一词来自retrieve,发音为/tri ...
- 自学JS
通过慕课网自学JS,敲了好多代码,好像没什么卵用,附上代码,再接再厉吧! //属性getter setter方法var man = {name : 'wsy', weibo : '@wsy', get ...
- swift 可选类型(optional)
可选类型定义 Swift 标准库中定义后缀 ?为可选类型 Optional<Wrapped> 的语法糖,这里语法糖可以简单理解为一种便捷的书写语法.也就是说,下面两个声明是等价的: va ...
- 数组去重,call、apply、bind之间的区别,this用法总结
一.数组去重,直接写到Array原型链上. //该方法只能去除相同的数字 不会去判断24和'24'是不同的 所有数字和字符串数字是相同是重复的 Array.prototype.redup=functi ...
- PHP中array_merge函数与array+array的区别
在PHP中可以使用array_merge函数和两个数组相加array+array的方式进行数组合并,但两者效果并不相同,下面为大家介绍两者具体的使用区别. 区别如下: 当下标为数值时,array_me ...
- 005---query接口初步
Query session.createQuery(String hql)方法; * hibernate的session.createQuery()方法是使用HQL(hibernate的查询语句)语句 ...
- LiteIDE灰调配色方案
说明 本文写于2017-04-03,使用LiteIDE X31(基于Qt 4.8.5),操作系统为Windows. 使用 LiteIDE下载后解压即可使用.配色方案的所有配置文件都位于liteide/ ...
- 浅谈MVC数据验证
一.一般情况 对于使用过MVC框架的人来说,对MVC的数据验证不会陌生,比如,我有一个Model如下: public class UserInfo { [Required(ErrorMessage = ...
- JavaSE教程-01初识Java
1.计算机的概念 软件+硬件 2.操作系统 Windows.Mac.Linux.Unix等 3.计算机编程语言 计算机语言是一种人与计算机沟通的媒介. 分类: 机器语言:都是基于二进制的方式,由0和1 ...
- 有趣而又被忽略的Unity技巧
0x00 前言 本文的内容主要来自YouTube播主Brackeys的视频TOP 10 UNITY TIPS 和TOP 10 UNITY TIPS #2.在此基础上经过自己的实践和筛选之后,选择了几个 ...