【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 ...
随机推荐
- Add Digits 解答
Question Given a non-negative integer num, repeatedly add all its digits until the result has only o ...
- windows 基于命令行制作vhd虚拟磁盘
什么是VHD? VHD是Virtual Hard Disk的简称,就是虚拟硬盘,就是能把VHD文件直接虚拟成一个硬盘,在其中能像真实硬盘一样操作,读取.写入.创建分区.格式化.如果你用过虚拟机,就会知 ...
- android面试题集1
Android 面试题(有详细答案) 附带答案,共100分 一.选择题(30题,每题1.5分,共45分) 1.java.io包中定义了多个流类型来实现输入和输出功能,可以从不同的角度对其进行分类,按功 ...
- 精讲N皇后问题
思想:存三个数组记录记录走的过程,运用回溯不符合或row==n+1就跳出当前层,直到找完:递归时的路径都在保存着,当连续跳出到第一次进入的dfs且i=n时就全部跳出dfs函数了: # ...
- 单调队列-hdu-4193-Non-negative Partial Sums
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4193 题目大意: 给n个数,a0,a1,...an,求ai,ai+1,...an,a1,a2,... ...
- Visual Studio® 2010 Web Deployment Projects站点编译生成bin同时发表插件
VS2010环境下: 1.Visual Studio® 2010 Web Deployment Projects下载地址: http://www.microsoft.com/downlo ...
- wpf msdn在线地址http://msdn.microsoft.com/zh-cn/library/ms752324(v=vs.110).aspx
http://msdn.microsoft.com/zh-cn/library/ms752324(v=vs.110).aspx
- java中23种设计模式
详情请看23种设计模式
- Android 的权限设置大全
android.permission.ACCESS_CHECKIN_PROPERTIES //同意读写訪问"properties"表在checkin数据库中.改值可以改动上传 an ...
- iOS UIScrollView的简单使用
本文代码下载 http://vdisk.weibo.com/s/BDn59yfnBVMAJ // // ViewController.m // ScrollView_T1119 // // Creat ...