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?
一个抠门的人要给一群孩子发糖果,在保证在相邻的孩子之间的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(糖果问题)的更多相关文章
- [LeetCode OJ] Candy
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- 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 ...
随机推荐
- C++切割字符串
std::string text = "2001_1;2005_5;"; std::stringstream ss(text); std::string sub_str; std: ...
- Spring AOP统一异常处理
1.添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- 20145307第四次JAVA学习实验报告
20145307实验四 Android开发基础 实验内容 基于Android Studio开发简单的 Android应用并部署测试; 了解Android组件.布局管理器的使用: 掌握Android中事 ...
- 20145310 《Java程序设计》第7周学习总结
20145310 <Java程序设计>第7周学习总结 教材学习内容总结 本周主要进行第十二章和第十三章的学习. Lambda定义:一个不用被绑定到一个标识符上,并且可能被调用的函数. 在只 ...
- java中的垃圾回收机
任何语言在运行过程中都会创建对象,也就意味着需要在内存中为这些对象在内存中分配空间,在这些对象失去使用的意义的时候,需要释放掉这些内容,保证内存能够提供给新的对象使用.对于对象内存的释放就是垃圾回收机 ...
- 1_jenkins环境搭建
前言 为什么要使用jenkins 可以实现批量部署.管理 支持常见的版本控制工具,git, svn等 发展成熟,使用范围广,容易找到解决方案 插件丰富,可以满足自己的需求 什么是jenkins 简单的 ...
- Python类变量,实例变量,类方法,实例方法,静态方法的分析
Python作为动态语言,跟静态语言如c/c++有很大区别,其中的一个重要的特性就是Python的变量无需声明直接可用.同样,类的成员变量无需声明,直接可用.目的是为了动态语言跟灵活,在思路想到的时候 ...
- python一个元素全为数的列表做差分
woc = [7, 5, 7, 3, 5, 1, 2] diff = [ wo[i]-wo[i+1] for i in range(len(wo)-1) ]
- stdclass
$item = new stdClass(); $item->goods_id = $item_goods->getID(); $item->goods_name ...
- jQuery中兄弟元素、子元素和父元素的获取
我们这里主要总结jQuery中对某元素的兄弟元素.子元素和父元素的获取,原声的Javascript代码对这些元素的获取比较麻烦一些,而jQuery正好对这些方法进行封装,让我们更加方便的对这些元素进行 ...