原题

前后两遍遍历

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. 栈内存不是只有2M吗?为什么不溢出?

    #include <stdio.h> #include <wchar.h> #include <stdlib.h> #define MAX_PATH 1024 FI ...

  2. QT---Native Wifi functions 应用(WiFi有密码连接)

    实现功能     无线网卡列表     无线热点扫面     无线连接(有密码,配置文件连接方式)     无线断开     重命名本地无线名(两种方式)     删除无线配置文件     开启和关闭 ...

  3. Oracle11超详细安装教程和配置

    这篇博客主要是介绍一下Oracle数据的安装过程和简单的配置,帮助大家可以简单的让Oracle运行起来,只是一个基础的教程. 准备工作: 如果你以前装过Oracle数据库,而且安装目录要改变请先打开注 ...

  4. Linux使用daemontools

    功能: 在使用memcached时候,怕因为一些不可预知的因素导致memcached进程死掉,而又不能及时的发现重启,可以通过daemontools来管理memcached的启动,当memcached ...

  5. SpringBoot(十九)_spring.profiles.active=@profiles.active@ 的使用

    现在在的公司用spring.profiles.active=@profiles.active@ 当我看到这个的时候,一脸蒙蔽,这个@ 是啥意思. 这里其实是配合 maven profile进行选择不同 ...

  6. Dynamics 365 for sales - Account与Contact之间的关系

    Account :可以理解成客户,可以是公司组织,当然也可以是个人 Contact: 从字面理解为联系人 例如,腾讯公司要买我们公司的软件,他们的主要负责人是IT部门的小马,那么,腾讯要创建成Acco ...

  7. java中更新文件时,指定原文件的编码格式,防止编码格式不对,造成乱码

    1.pom中添加引入cpdetector(一个可以自动检测文本编码格式的项目) //pom中添加引入cpdetector(一个可以自动检测文本编码格式的项目) <dependency> & ...

  8. auth-booster配置和使用(yii1.5)

    auth-booster这个是一个yii框架扩展中的一个模块.是非常好用的(但是里面的说明都是英文的,所以国人用还需要改一点里面的汉化) 1.下载auth-booster这个:http://www.y ...

  9. 带你手写基于 Spring 的可插拔式 RPC 框架(一)介绍

    概述 首先这篇文章是要带大家来实现一个框架,听到框架大家可能会觉得非常高大上,其实这和我们平时写业务员代码没什么区别,但是框架是要给别人使用的,所以我们要换位思考,怎么才能让别人用着舒服,怎么样才能让 ...

  10. Mac上PyCharm运行多进程报错的解决方案

    Mac上PyCharm运行多进程报错的解决方案 运行时报错 may have been in progress in another thread when fork() was called. We ...