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 ...
随机推荐
- 设置eclipse编码格式
1.修改eclipse默认工作空间编码方式.点击Window-->Preferences-->General-->Workspace,设置编码格式为UTF-8,然后点击OK.
- selenium的基本介绍
应吴姑娘(漂亮的姑娘)之邀,加上我师兄(屌丝)和国新(屌丝),组了个四黑小团伙,每周二分享点东西,感觉就是四个辣鸡相互取暖.可惜,今天早上直接是睡过去了,下午都捐给了<白夜追凶>---没办 ...
- 小练手:用HTML5 Canvas绘制谢尔宾斯基三角形
文章首发于我的知乎专栏,原地址:https://zhuanlan.zhihu.com/p/26606208 以前看到过一个问题:谢尔宾斯基三角形能用编程写出来么?该怎么写? - 知乎,在回答里,各方大 ...
- Ubuntu14.04+caffe+cuda7.5 环境搭建以及MNIST数据集的训练与测试
Ubuntu14.04+caffe+cuda 环境搭建以及MNIST数据集的训练与测试 一.ubuntu14.04的安装: ubuntu的安装是一件十分简单的事情,这里给出一个参考教程: http:/ ...
- mysql批量修改列类型-生成语句
SELECT CONCAT( 'alter table ', table_name, ' MODIFY COLUMN ', column_name, ' float DEFAULT NULL;' ) ...
- HDFS数据块
磁盘也是由数据块组成的,一般默认大小是512字节,构建磁盘之上的文件系统一般是磁盘块的整数倍. HDFS也是采用块管理的,但是比较大,在Hadoop1.x中默认大小是64M,Hadoo ...
- JavaScript 事件处理详解
事件绑定与解绑: el.onEventName = function (){} document.getElementById("dom").onclick = function( ...
- yum安装redis phpredis扩展
转载地址:http://blog.csdn.net/musicrabbit/article/details/9729941 redis和php-redis在官方源上是没有的,需要安装其他的源,其他源的 ...
- Python3.x:定义一个类并且调用
Python3.x:定义一个类并且调用 1,定一个类Shrjj(其中有属性:name, jjzt,fbsjj,etf,lof,fjlof): class Shrjj(object): def __in ...
- Jquery编历数组
<html> <head> <title>编历</title> <script type="text/javascript"& ...