LeetCode: 575 Distribute Candies(easy)
题目:
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].
代码:
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)的更多相关文章
- 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 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 ...
随机推荐
- soap的调用方式
1.方式1 url:http://localhost:3651/recruit/index.asmx?WSDL post 内容: <soapenv:Envelope xmlns:soape ...
- OpenCV 入门示例之二:播放 AVI 视频
前言 本文展示一个播放 AVI 视频的程序.( 呵呵是 AVI 视频不是 AV 视频噢! ) 代码示例 // 此头文件包含图像IO函数的声明 #include "highgui.h" ...
- android好博客
app集成支付宝.app缓存管理.app列表圆角设计.App自动更新之通知栏下载(有续).索引ListView.App数据格式之解析Json.拖拽ListView http://www.cnblogs ...
- contentprovider基础
程序启动后,只要再manifest当中注册上,就会执行PersonProvider()创建一个对象
- include vector 编译出错VC++
error C2665: “operator new” : 5个重载中没有一个可以转换参数1(从“const char [71]”类型)这个错误是怎么回事啊,搜索了整个项目好像没有可疑的new操作阿. ...
- 《unix环境高级编程》学习笔记【原创】
本文基于unix环境高级编程的学习的笔记,写的比较简如有不对,欢迎指点. 简单的描述下面函数的功能改变ctr+c信号原本的作用终止程序,在按下中断键的时候输出一句话. while循环主要读取用户的输入 ...
- javascrip中ajax
移动端对加载速度要求比较高,由于jquery插件有270多k,无形中增加加载的速度,下面整理一下原生js中ajax: 先了解ajax的基础知识 (1)XMLHttpRequest 对象 XMLHttp ...
- XML简介——可扩展标记语言(Extensible Markup Language)
(What) XML是什么? XML指可扩展标记语言(Extensible Markup Language) 1. XML是一种标记语言,类似HTML. 2. XML具有自我描述性 3. XML ...
- 谈谈嵌套for循环的理解
谈谈嵌套for循环的理解 说for的嵌套,先说一下一个for循环的是怎么用的. 这次的目的是为了用for循环输出一个乘法口诀表,一下就是我的一步步理解. 一. 语法: ...
- ubuntu 下编译安装ceph
git clone --recursive https://github.com/ceph/ceph.git cd ceph/ sudo apt-get install libtool sud ...