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. JVM性能调优(转)

    本文转自:http://www.cnblogs.com/chen77716/archive/2010/06/26/2130807.html 最近因项目存在内存泄漏,故进行大规模的JVM性能调优 , 现 ...

  2. c++对txt文件的读取与写入

    转自:http://blog.csdn.net/lh3325251325/article/details/4761575 #include <iostream> #include < ...

  3. 【Thinking in Java, 4e】控制流程执行

    p46~p75: [迭代] 1.Java不允许将数字作为布尔值用. 1.有点意思的小程序WhileTest. public class WhileTest { static boolean condi ...

  4. 寻路算法A*, JPS(跳点搜索)的一些杂谈

    A*是一个比较经典的启发式寻路算法.是基于dijkstra算法,但是加入了启发函数,使路径搜索效率更高.实现起来很简单.不过要做到通用性高,比如支持各种不同类型的地图,甚至不仅仅是地图,而是个图结构如 ...

  5. SqlMapConfig.xml配置

    总结自:https://blog.csdn.net/d582693456/article/details/79886780 SqlMapConfig.xml是mybatis的核心配置 properti ...

  6. git的应用

    对git的应用 (终于第一次用会git) 根据胡东晖同学的博客(使用git推送代码到开源中国以及IDEA环境下使用git)与热心指导,自己跟着做了,虽然途中出了很多很多问题,但是好在最后还是成功了!! ...

  7. 如何使用curl进行网页授权

    答:使用curl的-u选项,使用方法如下 curl -u username URL (会提示输入密码)

  8. 【python】argparse学习(转)

    点击这里成为作者 · 更新于 2018-11-14 21:00:36 argparse argparse 是 Python 内置的一个用于命令项选项与参数解析的模块,通过在程序中定义好我们需要的参数, ...

  9. JAVA 单向链表

    package com.session.link; /** * 单向链表 */public class LinkedList<T> { private Node head;//指向链表头节 ...

  10. Codeforces Round #365 (Div. 2) B - Mishka and trip

    http://codeforces.com/contest/703/problem/B 题意: 每个点都有一个值,每条边的权值为这两个点相乘.1~n成环.现在有k个城市,城市与其他所有点都相连,计算出 ...