[leetcode] 135. Candy (hard)
原题
前后两遍遍历
class Solution
{
public:
int candy(vector<int> &ratings)
{
vector<int> res;
int len = ratings.size();
for (int i = 0; i < len; i++)
{
res.push_back(1);
}
for (int j = 0; j < len; j++)
{
if (j > 0 && ratings[j] > ratings[j - 1])
{
res[j] += res[j - 1];
}
}
for (int k = len - 1; k >= 0; --k)
{
if (k < len - 1 && ratings[k] > ratings[k + 1] && res[k] <= res[k+1])
{
res[k] = res[k + 1]+1;
}
}
int sum = 0;
for (auto i : res)
{
cout<<i<<endl;
sum += i;
}
return sum;
}
};
[leetcode] 135. Candy (hard)的更多相关文章
- 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 N个孩子站成一排,给每个人设定一个权重
原文:http://www.cnblogs.com/AndyJee/p/4483043.html There are N children standing in a line. Each child ...
- 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 当递减序列结束时,如果少分了糖果 ...
- Java for LeetCode 135 Candy
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- [Leetcode 135]糖果分配 Candy
[题目] There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- 【LeetCode】135. Candy
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- [LeetCode][Java]Candy@LeetCode
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- 135. Candy
题目: There are N children standing in a line. Each child is assigned a rating value. You are giving c ...
随机推荐
- MySQL中临时表的基本创建与使用教程(CREATETEMPORARY TABLE)
当工作在非常大的表上时,你可能偶尔需要运行很多查询获得一个大量数据的小的子集,不是对整个表运行这些查询,而是让MySQL每次找出所需的少数记录,将记录选择到一个临时表可能更快些,然后在这些表运行查询. ...
- 无辜的RAD(RAD是让你去创造和使用可复用的组件,不是让程序员“变白痴”)good
无辜的RAD 2005-3-21 说实话,RAD很无辜.从出生的那天其就被骂,天天被指着鼻子说“不就是拖个控件嘛”,就好像当年说学电脑“不就是插个鼠标嘛”.也怪程序员大都天性犯贱,就爱一遍又一便的写基 ...
- Java的String类字符串的拆分
在java编程中,有时候我们需要把一个字符串按照某个特定字符.字母等作为截点分割这个字符串, 这样我们就可以使用这个字符串的一部分或者把所有截取的内容保存到数组里等操作. public class S ...
- 揭秘重度MMORPG手游后台性能优化方案
本文节选自<2018腾讯移动游戏技术评审标准与实践案例>手册,由腾讯互娱工程师王杰分享<仙剑奇侠传online>项目中游戏后台的优化经验,深度解析寻路算法.视野管理.内存优化. ...
- vue 开发webapp 手机返回键 退出问题
vue 开发webapp 手机返回键 退出问题 mui进行手机物理键的监听 首先安装 vue-awesome-mui npm i vue-awesome-mui 在main.js注册 在index.h ...
- kafka 名词概念
ProducerConsumerBrokerTopicPartitionConsumer Group分布式 Broker Kafka集群包含一个或多个服务器,这种服务器被称为brokerTop ...
- Thread中的start()方法和自己定义的run()方法有什么区别
在讲这个问题之前引入一下多线程的小知识吧 /*/windows系统中的应用程序来做说明 ,例如:扫雷程序,游戏进行的同时,可以同时记录分数,计算时间等. 其实一个应用程序就是一个可执行文件,中包含了一 ...
- CSS3边框与圆角
1. CSS3 圆角 border-radius 属性 一个最多可指定四个border -*- radius属性的复合属性,这个属性允许你为元素添加圆角边框!语法:border-radius: 1-4 ...
- 并发编程-concurrent指南-原子操作类-AtomicBoolean
类AtomicBoolean
- 分组在re模块中的使用
import re #search s = "<a>wahaha</a>" #标签语言 html 和 web相关 ret= re.search(" ...