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

You are giving candies to these children subjected to the following requirements:

  • Each child must have at least one candy.
  • Children with a higher rating get more candies than their neighbors.

What is the minimum candies you must give?

一个抠门的人要给一群孩子发糖果,在保证在相邻的孩子之间的rating较高者应该比rating低的获得更多的糖果的情况下,怎样发最少的糖果。

左右各遍历一次即可,代码如下所示:

 class Solution {
public:
int candy(vector<int>& ratings) {
int sz = ratings.size();
vector<int>candy(sz, );
if(sz == ) return ;
if(sz == ) return ;
candy[] = ;
for(int i = ; i < sz; ++i){
if(ratings[i] > ratings[i-])
candy[i] = candy[i-] + ;
else
candy[i] = ;
}
for(int i = sz-; i >= ; i--){
if(ratings[i] > ratings[i+])
if(candy[i] <= candy[i+])
candy[i] = candy[i+] + ;
}
int sum = accumulate(candy.begin(), candy.end(), );
return sum;
}
};

LeetCode OJ:Candy(糖果问题)的更多相关文章

  1. [LeetCode OJ] Candy

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

  2. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  3. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  4. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

  5. LeetCode OJ学习

    一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...

  6. LeetCode OJ 297. Serialize and Deserialize Binary Tree

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  7. 备份LeetCode OJ自己编写的代码

    常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...

  8. LeetCode OJ 之 Maximal Square (最大的正方形)

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...

  9. LeetCode OJ:Integer to Roman(转换整数到罗马字符)

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  10. LeetCode OJ:Serialize and Deserialize Binary Tree(对树序列化以及解序列化)

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

随机推荐

  1. HBase1.2.6 预分区后,数据不进入预定分区的一个 bug

    rowkey 如下: 19000015115042900001511504390000151150449000015115045900001511504690000151150479000015115 ...

  2. 20145316 《Java程序设计》 课程总结

    ###20145316许心远<Java学习笔记(第8版)>课程总结 ##每周读书笔记链接汇总 ▪    [第一周读书笔记](http://www.cnblogs.com/xxy745214 ...

  3. 摄像头PIN脚功能作用

    摄像头PIN脚功能作用,Camera硬件系统分析 9 f  E+ E2 b  N. j4 M2 U- a. q9 A) T# c& O& C% x+ l5 l! q           ...

  4. 【Python】闭包 & 匿名函数

    闭包 1.注意:返回的函数内部不要使用后续会发生变化的变量. def f(): gs = [] for k in range(1, 4): def g(i): return i + k gs.appe ...

  5. Web安全学习笔记之Nmap扫描原理与用法

    1     Nmap介绍 Nmap扫描原理与用法PDF:下载地址 Nmap是一款开源免费的网络发现(Network Discovery)和安全审计(Security Auditing)工具.软件名字N ...

  6. Codeforces679E. Bear and Bad Powers of 42

    传送门 今天子帧的一套模拟题的T3. 考试的时候其实已经想到了正解了,但是一些地方没有想清楚,就没敢写,只打了个暴力. 首先考虑用线段树维护区间信息. 先把每个值拆成两个信息,一是距离他最近的且大于他 ...

  7. MySQL优化具体

    1. 查询与索引优化分析 在优化MySQL时,通常需要对数据库进行分析,常见的分析手段有慢查询日志,profiling分析,EXPLAIN分析查询,以及show命令查询系统状态及系统变量,通过定位分析 ...

  8. 【前端】用javaScript实现实现一个球池的效果

    ballPool 用javaScript实现实现一个球池的效果,实现效果如下所示: 动图: 截图: HTML代码: <!DOCTYPE html> <html > <he ...

  9. slf4j日志框架绑定机制

    一.环境搭建 我们以log4j为例,探寻slf4j与log4j的绑定过程. 1.Java类 public class Slf4jBind { public static void main(Strin ...

  10. 安装xenserver6.5

    安装之前的准备工作就不在赘述了,下边开始安装 1.安装界面 2.键盘模式选择默认-美式,ok 3.这里提醒你安装将清除磁盘信息,做好备份,选择ok. 4.这一步没有选择,要么继续,要么退回,选择Acc ...