原题

前后两遍遍历

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. OpenSSL的命令行用法,以及参数大全

    c:\openssl\bin>opensslOpenSSL> versionOpenSSL 1.0.2j 26 Sep 2016OpenSSL> https://wiki.opens ...

  2. 解决C/C++程序执行一闪而过的方法(使用getchar,或者cin.get,不推荐system(“pause”))

    简述 在VS编写控制台程序的时候,包括使用其他IDE(Visual C++)编写C/C++程序,经常会看到程序的执行结果一闪而过,要解决这个问题,可以在代码的最后加上system(“pause”).g ...

  3. HttpWebRequest 在出错时获取response内容

    HttpWebRequest 请求时,服务器会返回500  501这些错误 并包含错误信息,通过如下代码可以拿到错误信息 HttpWebResponse res; try { res = (HttpW ...

  4. BFS提高效率的一点建议

    BFS有两种常见的形式: 形式1: 把初始点加入队列; while (队列非空) { 取出队头; 操作取出的点; 寻找周围符合条件的点加入队列; } 形式2: 操作初始点 把初始点加入队列; whil ...

  5. web页面的时间传入servlet如何转换为可以存入MySQL的Date类型

    在web页面中当使用如下语句: <input type="date" name="startTime"/> 提交到servlet中 在servlet ...

  6. 你需要了解的HTTP协议

    了解HTTP协议 HTTP (超文本传输协议,HyperText Transfer Protocol),是一种用于分布式.协作式和超媒体信息系统的应用层协议.HTTP 是万维网的数据通信基础. 通常, ...

  7. CentOS7使用firewalld防火墙

    firewalld的基本使用 启动: systemctl start firewalld 关闭: systemctl stop firewalld 查看状态: systemctl status fir ...

  8. Java连载2-Java特性

    一.JDK 1.含义:Java开发工具包. 2.做Java开发之前必须安装的一个工具包,​下载地址:https://www.oracle.com/index.html 3.Java包括三大块内容: ( ...

  9. C语言:正负数之间取模运算(转载)

    如果 % 两边的操作数都为正数,则结果为正数或零:如果 % 两边的操作数都是负数,则结果为负数或零.C99 以前,并没有规定如果操作数中有一方为负数,模除的结果会是什么.C99 规定,如果 % 左边的 ...

  10. 003.SQLServer数据库镜像高可用部署

    一 数据库镜像部署准备 1.1 数据库镜像支持 有关对 SQL Server 2012 中的数据库镜像的支持的信息,请参考:https://docs.microsoft.com/zh-cn/previ ...