135. Candy(Array; Greedy)
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?
思路:neighbor=>元素值与前、后元素有关=>
第一次前向扫描:保证high rate元素比左邻居糖多;
第二次后向扫描:保证high rate元素比右邻居糖多;
class Solution {
public:
int candy(vector<int> &ratings) {
vector<int> new_ratings(ratings.size(), );
for(int i = ; i < ratings.size(); i++) //for the first time, scan from beginning
{
if(ratings[i] > ratings[i-])
{
new_ratings[i] = new_ratings[i-]+;
} }
for(int i=ratings.size()-;i>=;i--){ //for the second time, scan from the end
if(ratings[i]>ratings[i+]&&new_ratings[i]<=new_ratings[i+]){
new_ratings[i]=new_ratings[i+]+;
}
}
int sum=;
for(int i=;i<new_ratings.size();i++){
sum+=new_ratings[i];
} return sum;
}
};
135. Candy(Array; Greedy)的更多相关文章
- LeetCode 135 Candy(贪心算法)
135. Candy There are N children standing in a line. Each child is assigned a rating value. You are g ...
- leetcode 135. Candy ----- java
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- Leetcode#135 Candy
原题地址 遍历所有小孩的分数 1. 若小孩的分数递增,分给小孩的糖果依次+12. 若小孩的分数递减,分给小孩的糖果依次-13. 若小孩的分数相等,分给小孩的糖果设为1 当递减序列结束时,如果少分了糖果 ...
- (LeetCode 135) Candy N个孩子站成一排,给每个人设定一个权重
原文:http://www.cnblogs.com/AndyJee/p/4483043.html There are N children standing in a line. Each child ...
- 135. Candy
题目: There are N children standing in a line. Each child is assigned a rating value. You are giving c ...
- [leet code 135]candy
1 题目 There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- 55. Jump Game (Array; Greedy)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 【LeetCode】135. Candy
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- Java for LeetCode 135 Candy
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
随机推荐
- osquery简单试用
备注: osquery facebook 开源的将操作系统指标转换为sql 查询,方便好用,很适合devops 性能分析,系统监控 1. 安装 参考 https://osquery.io/downl ...
- 关于时间戳和QDateTime相互转换的有关问题(转)
1.toTime_t()把2014年12月19日10:24:40这样的QDateTime的格式转变为1418955940这样的时间戳 QDateTime time = QDateTime::curre ...
- bzoj 2535 && bzoj 2109 [Noi2010]Plane 航空管制——贪心
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2535 https://www.lydsy.com/JudgeOnline/problem.p ...
- JavaFX 之窗口跳转(一)
一.前言 笔者此处不讲JavaFX的基础API,只针对笔者工作时遇到的问题进行记录与总结. 零基础的网友可以访问 http://www.javafxchina.net/blog/docs/tutori ...
- linux误删数据恢复
linux下数据恢复工具有: 1.通过分析文件系统的日志,解析文件的inode,可以恢复ex3 ex4的文件系统下的数据 extundelete:扫描inode和恢复数据同时进行,因此恢复速度很快.支 ...
- zabbix 布署实践【8 监控windows server】
参考http://www.cnblogs.com/likehua/p/3968689.html的思路,我安装的是zabbix 3.0 从zabbix官网下载windown的 Zabbix pre-co ...
- js 日期正则 转载
天秤水的Blog 博客园 博问 闪存 首页 新随笔 联系 管理 订阅 随笔- 70 文章- 0 评论- 74 最强日期正则表达式 一.简单的日期判断(YYYY/MM/DD): ^\d{ ...
- 数据结构与算法JavaScript描述——队列
注:澄清一个bug: /** * 删除队首的元素: */ function dequeue(){ return this.dataStore.shift(); } 应该有return: 队列是一种 ...
- python md5 请求 构造
-----------------md5加密的方法:---------------------------------- import hashlib m = hashlib.md5() ...
- tomcat 关闭出现 Connection refused 解决方法
1.找到tomcat占用的端口 ps -eaf|grep tomcat 2.杀掉tomcat进程 kill -p 6453 后记: 杀其他进程的时候,上面的方法不可以,用下面的就ok了 lsof - ...