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. 如何通过包名打开手机里的APP

    目前已知的打开APP的方式有两种, 一种是通过openUrl打开,这种有一个严重的问题,即必须添加白名单,白名单之外的APP即时安装了也无法打开. 另一种就是今天的重点,通过包名打开APP.先上核心代 ...

  2. ServletConfig获取Servlet的公共参数方法

    web.xml配置: <servlet> <servlet-name>AServlet</servlet-name> <servlet-class>AS ...

  3. 关于iOS UIWebView 加载网页,点击网页内某些控件导致 Application 'UIKitApplication:xxx.xxx.xxx' was killed by jetsam.

    问题:公司用的腾讯问卷系统,内嵌在我们应用或游戏的自定义UIWebView里面展示,发现在iOS 10 以下系统,点击圆形勾选框 会大概率出现闪退. 通过联调发现:报了这样一个警告Applicatio ...

  4. 利用C++调用天气webservice-gSOAP方法

    首先需要下载一个gSOAP工具包 下载路径为:https://sourceforge.NET/projects/gsoap2/ 至于有关于gSOAP的一些用法和包含的文件的说明可从官网查看:http: ...

  5. Python学习笔记之函数参数传递 传值还是传引用

      在学完Python函数那一章节时,很自然的的就会想到Python中函数传参时传值呢?还是传引用?或者都不是? 在回答上面的问题之前我们先来看看下面的代码: 代码1: def foo(var): v ...

  6. 20145312 《Java程序设计》第五周学习总结

    20145312 <Java程序设计>第五周学习总结 学习笔记 Chapter8 异常处理 8.1语法与继承架构 1.Java中的错误以对象方式呈现,只要捕捉包装错误的对象,就可以针对该错 ...

  7. linux下tar的使用方法

    1.仅打包 tar -cvf hello.tar hello (输出文件大小为10240) 2.打包后压缩成gzip压缩格式 tar -czvf hello.tar.gz hello (输出文件大小为 ...

  8. Codeforces - 828E DNA Evolution —— 很多棵树状数组

    题目链接:http://codeforces.com/contest/828/problem/E E. DNA Evolution time limit per test 2 seconds memo ...

  9. 2017 ACM/ICPC Asia Regional Qingdao Online - 1011 A Cubic number and A Cubic Number

    2017-09-17 17:12:11 writer:pprp 找规律,质数只有是两个相邻的立方数的差才能形成,公式就是3 * n * (n + 1) +1, 判断读入的数是不是满足 这次依然只是做了 ...

  10. 2017ACM/ICPC广西邀请赛-重现赛1005 CS course

    2017-08-31 16:19:30 writer:pprp 这道题快要卡死我了,队友已经告诉我思路了,但是做题速度很缓慢,很费力,想必是因为之前 的训练都是面向题解编程的缘故吧,以后不能这样了,另 ...