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?

 class Solution {
public:
int candy(vector<int> &ratings) {
vector<int> candy_number(ratings.size(), );
//不用迭代器,改用下标索引实现,这样代码看起来更加简洁
for(int i=; i<ratings.size(); ++i)
{
//保障当右边的rating比左边的高时,则右边分得的糖果比左边多
if( ratings[i] > ratings[i-]) //eg:输入[4 2 3 4 1],最少的分法是[2 1 2 3 1],分发9个糖果
candy_number[i] = (candy_number[i] > candy_number[i-]+) ? candy_number[i] : candy_number[i-]+;
} for(int i=ratings.size()-; i>=; --i)
{
//保障当左边的rating比右边的高时,则左边分得的糖果比右边多
if( ratings[i] > ratings[i+])
candy_number[i] = (candy_number[i] > candy_number[i+]+) ? candy_number[i] : candy_number[i+]+;
} int total=;
for(int i=; i!=ratings.size(); ++i)
total = total + candy_number[i]; return total;
}
};

[LeetCode OJ] Candy的更多相关文章

  1. LeetCode OJ 题解

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

  2. 【LeetCode OJ】Interleaving String

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

  3. 【LeetCode OJ】Reverse Words in a String

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

  4. LeetCode OJ学习

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

  5. 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 ...

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

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

  7. 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 ...

  8. 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 ...

  9. 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. JVM上的随机数与熵池策略

    在apache-tomcat官方文档:如何让tomcat启动更快里面提到了一些启动时的优化项,其中一项是关于随机数生成时,采用的“熵源”(entropy source)的策略. 他提到tomcat7的 ...

  2. 【HDOJ】3419 The Three Groups

    记忆化搜索. /* 3419 */ #include <cstdio> #include <cstring> #include <cstdlib> #define ...

  3. hdu-3487-Play with Chain-(splay 区间翻转,切割,插入)

    题意: 区间翻转,切割,插入 // File Name: ACM/HDU/3487.cpp // Author: Zlbing // Created Time: 2013年08月10日 星期六 21时 ...

  4. MVVM模式中WPF数据的完全绑定

    一:截图,描述:将后台代码的姓名.年龄绑定到文本框,单击”增加年龄“--年龄自+1,单击”显示年龄“--弹出年龄的显示对话框,实现了从文本框修改年龄和后台更改年龄并显示到文本框 运行结果和解决方案管理 ...

  5. N-Queens II——Leetcode

    Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...

  6. c#反序列化

    C#序列化(转载) 2011-03-15  |  凯之风  |  转藏(2) 序列化有下列要求: A> 只序列化PUBLIC的成员变量和属性 B> 类必须有默认识构造函数 C> 如果 ...

  7. 《Euclidea3》-Eta-07

    Q: 分析:考虑到充分利用三等分和角度的信息,这里我们只需做出一个36°的角即可. 考虑一个顶角是36°的等腰三角形.如下图. 设AD=a1,CD=a2,根据相似,易得a1:a2=(√5-1)/2. ...

  8. 带你一分钟理解 JavaScript 闭包

    什么是闭包? 先看一段代码: function a(){ var n = 0; function inc() { n++; console.log(n); } inc(); inc(); } a(); ...

  9. poj 1458 Common Subsequence【LCS】

    Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43132   Accepted: 17 ...

  10. 对Spring的一些理解

    最近在复习一些关于框架的概念性问题,主要是为了最近的面试,怕被问到这些概念性的问题.不过在真正做开发的时候还是要好好理解这些框架的基本原理,以及它们的工作流程.这样,我们才能更好的使用这些框架.下面就 ...