原题地址

遍历所有小孩的分数

1. 若小孩的分数递增,分给小孩的糖果依次+1
2. 若小孩的分数递减,分给小孩的糖果依次-1
3. 若小孩的分数相等,分给小孩的糖果设为1

当递减序列结束时,如果少分了糖果,就补上,如果多分了糖果,就减掉

究竟补多少或减多少,这很容易计算,不啰嗦了。

时间复杂度:O(n)

代码:

 int candy(vector<int> &ratings) {
int n = ratings.size();
int sum = ;
int len = ;
int prev = ; for (int i = ; i < n; i++) {
if (ratings[i] == ratings[i - ]) {
prev = ;
len = ;
sum += prev;
}
else if (ratings[i] > ratings[i - ]) {
prev++;
len = ;
sum += prev;
}
else if (ratings[i] < ratings[i - ]) {
prev--;
len++;
sum += prev;
if (i == n - || ratings[i] <= ratings[i + ]) {
sum += prev < ? ( - prev) * (len + ) : ( - prev) * len;
prev = ;
}
}
} return sum;
}

Leetcode#135 Candy的更多相关文章

  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. Java for LeetCode 135 Candy

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

  5. [leetcode] 135. Candy (hard)

    原题 前后两遍遍历 class Solution { public: int candy(vector<int> &ratings) { vector<int> res ...

  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. freefilesync 7 使用

    官方下载地址:http://www.freefilesync.org/download.php 1.打开FreeFileSync 设置左右的文件夹,设置过滤规则,设置同步方式(双向.单向),执行同步 ...

  2. [译]Cassandra的数据读写与压缩

    本文翻译主要来自Datastax的cassandra1.2文档.http://www.datastax.com/documentation/cassandra/1.2/index.html.此外还有一 ...

  3. 十天学会单片机Day2键盘检测(独立键盘、矩阵键盘)

    1.键盘的分类 编码键盘:键盘上闭合键的识别由专用的硬件编码器实现,并产生键编码号或键值的称为编码键盘,如计算机键盘 非编码键盘:靠软件编程来识别的称为非编码键盘.独立键盘.矩阵键盘 2.按键消抖   ...

  4. Python多版本安装 Python2.7和Python3.5

    声明:本文仅仅在win8.1测试通过! 1.下载 Python2.7,3.5 2.依次安装Python27(c:\Python27)  Python35(c:\Python35) 3.c:\Pytho ...

  5. 7.css盒模型

    所谓的盒模型,其实就是把元素当成盒子,元素里的文本就是盒子里的东西. 而根据元素的特效,其盒模型的特效也不同,下面是一些总结: 1.块级元素(区块) 所谓块级元素,就是能够设置元素尺寸.有隔离其他元素 ...

  6. JLINK V8 Keil MDK4.10 STM32

    新买的JLINK v8仿真器,第一次使用,编译环境是Keil MDK4.10,目前芯片是STM32F103x. 按照光盘的说明先安装了驱动,USB接上JLINK v8,显示驱动成功.但是在debug或 ...

  7. 【微网站开发】之微信内置浏览器API使用

    最近在写微网站,发现了微信内置浏览器的很多不称心的地方: 1.安卓版的微信内浏览器底部总是出现一个刷新.前进.后退的底部栏,宽度很大,导致屏幕显示尺寸被压缩 2.分享当前网站至朋友圈时,分享的图片一般 ...

  8. NOJ1142-最大连续和

    最大连续和 时间限制(普通/Java) : 1000 MS/ 3000 MS          运行内存限制 : 65536 KByte总提交 : 1282            测试通过 : 230 ...

  9. ociuldr 支持分多个数据文件

    在审计工作,将几亿条的oracle数据通过sqlserver自带工具导入到sqlserver中,速度不是特别的理想,虽然通过视图方式能提高一些速度,但是既不简洁,也不方便. 用ociuldr工具,可以 ...

  10. CentOS 7 + nginx + uwsgi + web2py (502 bad gateway nginx)

    Web2py开发包中自带的setup-web2py-nginx-uwsgi-centos64.sh脚本, 只能运行在CentOS 6.4中使用, 如果直接在CentOS 7 中使用该脚本布署后, 访问 ...