[LeetCode OJ] Candy
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的更多相关文章
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
- LeetCode OJ学习
一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...
- 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 ...
- 备份LeetCode OJ自己编写的代码
常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 安装Maven、nexus
一.软件包版本 jdk:jdk1.7.0_79 maven:apache-maven-3.3.3-bin.tar.gz nexus:nexus-webapp-2.8.0-05.war 二.安装mave ...
- 字符串(后缀数组):POJ 3415 Common Substrings
Common Substrings Description A substring of a string T is defined as: T(i, k)=TiTi+1...Ti+k-1, 1≤ ...
- 数学概念——J - 数论,质因数分解
J - 数论,质因数分解 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- WCF扩展系列 - 行为扩展(Behaviors)
原文地址:http://www.cnblogs.com/Creator/archive/2011/05/21/2052687.html 这个系列的第一部分将会重点关注WCF行为(behaviors), ...
- LeetCode (85): Maximal Rectangle [含84题分析]
链接: https://leetcode.com/problems/maximal-rectangle/ [描述] Given a 2D binary matrix filled with '0's ...
- 配置Chrome启动参数支持本地AJAX请求,解决XMLHttpRequest cannot load file..,Uncaught NetworkError: Failed to execute 'send' on 'XMLHttpRequest':.. 问题
直接将本地的HTML文件拖拽到Chrome浏览器中运行时,发送的AJAX请求本地文件,会报跨域错误: 报错的原因是:Chrome默认不支持本地的AJAX请求! 解决问题的办法是:给Chrome浏览器添 ...
- python数据的存储和持久化操作
Python的数据持久化操作主要是六类:普通文件.DBM文件.Pickled对象存储.shelve对象存储.对象数据库存储.关系数据库存储. 普通文件不解释了,DBM就是把字符串的键值对存储在文件里: ...
- quartz 的学习和使用。
任务调度器, 定时任务,保存好后会被放入触发器,这些触发器被存入到数据库,调度器线程扫描,如果有待触发的打开锁,拿到job信息,更改trigger信息,释放锁,返回所有的trigger列表,再 按照时 ...
- slides 带手势的图片滑动效果(用于移动终端)
slidesjs 是基于jQuery开发的一款功能强大,是简单的幻灯片插件,但是需要要应用于移动终端的话,还需要考虑手势滑动时候图片切换功能. 此次,我就在slidesjs基础上扩展了两个swipe属 ...
- CSRF——攻击与防御
CSRF——攻击与防御 author: lake2 0x01 什么是CSRF攻击 CSRF是Cross Site Request Forgery的缩写(也缩写为XSRF),直译过来就是跨站请求伪造的意 ...