原题

前后两遍遍历

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)的更多相关文章

  1. LeetCode 135 Candy(贪心算法)

    135. Candy There are N children standing in a line. Each child is assigned a rating value. You are g ...

  2. (LeetCode 135) Candy N个孩子站成一排,给每个人设定一个权重

    原文:http://www.cnblogs.com/AndyJee/p/4483043.html There are N children standing in a line. Each child ...

  3. leetcode 135. Candy ----- java

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

  4. Leetcode#135 Candy

    原题地址 遍历所有小孩的分数 1. 若小孩的分数递增,分给小孩的糖果依次+12. 若小孩的分数递减,分给小孩的糖果依次-13. 若小孩的分数相等,分给小孩的糖果设为1 当递减序列结束时,如果少分了糖果 ...

  5. Java for LeetCode 135 Candy

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

  6. [Leetcode 135]糖果分配 Candy

    [题目] There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  7. 【LeetCode】135. Candy

    Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  8. [LeetCode][Java]Candy@LeetCode

    Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  9. 135. Candy

    题目: There are N children standing in a line. Each child is assigned a rating value. You are giving c ...

随机推荐

  1. Linux下卸载QT SDK

    unbuntu下卸载QT方法一:you can remove it like this, those developers should add this somewhere ! like next ...

  2. easyui在IE中: SCRIPT1003: 缺少 ':'

  3. Memcached在Linux系统下的安装和PHP开启 Memcached的 扩展 超级解决方案

    [项目背景]:阿里云ECS服务器,Linux(centos7.2 64位),环境部署使用的是阿里云一键安装包(LAMP)等 [项目需求]:linux安装memcached 和php开启Memcache ...

  4. Codility--- TapeEquilibrium

    Task description A non-empty zero-indexed array A consisting of N integers is given. Array A represe ...

  5. mybatis链接数据库

    DBTools类 public class DBTools { // 加载mybatis文件 public static SqlSession getSession() { //加载配置文件 Inpu ...

  6. SpringCloud系列——TX-LCN分布式事务管理

    前言 SpringCloud分布式架构给我们带来开发上的便利,同时增加了我们对事务管理的难度,微服务的遍地开花,本地事务已经无法满足分布式的要求,由此分布式事务问题诞生. 分布式事务被称为世界性的难题 ...

  7. 系列教程 - java web开发

    代码之间工作室持续推出Java Web开发系列教程与案例,供广大朋友分享交流技术经验,帮助喜欢java的朋友们学习进步: java web 开发教程(1) - 开发环境搭建 技术交流QQ群: 商务合作 ...

  8. 接口测试中读取excel中的请求数据含有中文问题,UnicodeEncodeError: 'latin-1' codec can't encode character '\u5c0f' in position

    错误信息:UnicodeEncodeError: 'latin-1' codec can't encode character '\u5c0f' in position 31: Body ('小') ...

  9. Hadoop 学习之路(六)—— HDFS 常用 Shell 命令

    1. 显示当前目录结构 # 显示当前目录结构 hadoop fs -ls <path> # 递归显示当前目录结构 hadoop fs -ls -R <path> # 显示根目录 ...

  10. 【通过ssh oracle11g安装】centos6静默安装oracle11g

    转载链接:https://blog.csdn.net/u011391839/article/details/76566316 根据实际情况略作调整~ 博主参考了以上链接成功进行安装,列一下自己遇到的坑 ...