【LeetCode练习题】Candy
分糖果
There are N children standing in a line. Each child is assigned a rating value.
You are giving candies to these children subjected to the following requirements:
- Each child must have at least one candy.
- Children with a higher rating get more candies than their neighbors.
What is the minimum candies you must give?
题目意思:
有N个小孩站成一条直线,每一个小孩都有一个等级的属性。按下面要求给小孩分糖果:
- 每一个小孩都必须有至少一个糖。
- 相邻的等级高的小孩得到更多的糖果。(相对他的前后的人)
求你最少要给出去多少个糖果。
解题思路:
N个小孩加上属性的话,有点类似于骆驼背的那种形状。我们新建一个用来表示每个小孩得多少糖果的大小为N的vector,和题目中给的等级vector对应。
然后两次遍历原有的vector,第一遍从前面向后面遍历,第二遍从后往前。
第一遍的目的是保证队伍后面的人如果比前面的人等级高的话,后面的人的糖果比前面的人多一个,否则后面的人糖果设为1个。
但是假如有类似于 3 4 5 4 3 这种等级分布的话,第一遍遍历后result变成了 1 2 3 1 1 ,而事实上应该是 1 2 3 2 1 ,第一遍遍历时没有考虑到后两个应该是递减的关系,所以第二遍遍历的目的就是处理这种本来应该是递减顺序全都成了1的情况。
代码如下:
class Solution {
public:
int candy(vector<int> &ratings) {
int len = ratings.size();
vector<int> result(len,);
int ret = ;
if(len == ){
return ret;
}
//第一遍
for(int i = ; i < len; i++){
if(ratings[i] > ratings[i-]){
result[i] = result[i-] +;
}
}
//第二遍
ret = result[len - ];
for(int i = len - ; i >= ; i--){
if(ratings[i] > ratings[i+]){
result[i] = max(result[i],result[i+]+);
}
ret += result[i];
}
vector<int>::iterator it = result.begin();
for(; it != result.end();it++){
cout << *it << "--";
}
return ret;
}
};
【LeetCode练习题】Candy的更多相关文章
- 【LeetCode练习题】Permutation Sequence
Permutation Sequence The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and ...
- Leetcode练习题Remove Element
Leetcode练习题Remove Element Question: Given an array nums and a value val, remove all instances of tha ...
- [LeetCode][Java]Candy@LeetCode
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- (LeetCode 135) Candy N个孩子站成一排,给每个人设定一个权重
原文:http://www.cnblogs.com/AndyJee/p/4483043.html There are N children standing in a line. Each child ...
- [LeetCode] 723. Candy Crush 糖果消消乐
This question is about implementing a basic elimination algorithm for Candy Crush. Given a 2D intege ...
- LeetCode 723. Candy Crush
原题链接在这里:https://leetcode.com/problems/candy-crush/ 题目: This question is about implementing a basic e ...
- [LeetCode] 723. Candy Crush 糖果粉碎
This question is about implementing a basic elimination algorithm for Candy Crush. Given a 2D intege ...
- LeetCode 135 Candy(贪心算法)
135. Candy There are N children standing in a line. Each child is assigned a rating value. You are g ...
- 【leetcode】Candy(hard) 自己做出来了 但别人的更好
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
随机推荐
- Linux系统编程(2)——文件与IO之系统调用与文件IO操作
系统调用是指操作系统提供给用户程序的一组"特殊"接口,用户程序可以通过这组"特殊"接口来获得得操作系统内核提供的特殊服务.在linux中用户程序不能直接访部内核 ...
- 【转】 linux内核移植和驱动添加(三)
原文网址:http://blog.chinaunix.net/uid-29589379-id-4708909.html 原文地址:linux内核移植和驱动添加(三) 作者:genehang 四,LED ...
- sroHOBOorz来自HOBO的高精类
/* bigint() bigint(long long) bigint(bigint) bigint(char*) bigint(string) +, +=, ++ -, -=, -- *, *= ...
- 用Python实现九九乘法表
1.用“#”组成的矩形的实现 代码 eight = int(input("Height:")) #用户输入高度 width = int(input("Width:&quo ...
- Java String类具体解释
Java String类具体解释 Java字符串类(java.lang.String)是Java中使用最多的类,也是最为特殊的一个类,非常多时候,我们对它既熟悉又陌生. 类结构: public fin ...
- 【巧妙算法系列】【Uva 11464】 - Even Parity 偶数矩阵
偶数矩阵(Even Parity, UVa 11464) 给你一个n×n的01矩阵(每个元素非0即1),你的任务是把尽量少的0变成1,使得每个元素的上.下.左.右的元素(如果存在的话)之和均为偶数.比 ...
- Android服务端本地窗口FramebufferNativeWindow
Android窗口系统 我们知道Android系统采用OpenGL来绘制3D图形,OpenGL ES提供了本地窗口(NativeWindow)的概念,无论是在Android平台中还是其他平台中,只要实 ...
- Java使用默认浏览器打开指定URL
直接贴代码: 方法一: Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler http://www.baidu.co ...
- SQL SERVER中如何格式化日期(转)
原文地址:http://blog.sina.com.cn/s/blog_95cfa64601018obo.html 1. SELECT convert(varchar, getdate(), 10 ...
- Sublime+Emmet
Sublime使用Package Control安装Emmet插件: 按Ctrl+Shift+P命令板 输入install然后选择install Package,然后输入emmet找到 Emmet C ...