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?

题意:给定无序的数列,保证大的比两侧中小的得到的糖果多,每个至少有一个

思路:正反遍历两次,正向遍历(从左往右)时,若后者比前者大,后者在前者的基础上加1;反向遍历,后者比前者小,前者加1。正向遍历一次,还是比较好想到的,那为什么要遍历两次,若整个数列是递减数列,则若只正向遍历一遍,所有得到的糖果都是1,这显然不符合题意,如4,3,2,1 ,其实只要开始时降序的就不行。那么方向遍历过程中,是不是只要前者比后者大就加1了?不是,我们举个反例“4,1,2,1”,正向遍历是,这四个小孩的得到的糖果数是:1,1,2,1,那么反向遍历时,若只要前者比后者大就加1会得到: 2,1,3,1,这显然不符合最少糖果的要求,最少应为2,1,2,1,这是因为正向遍历中,数组A[2]对应的元素值比两边都大,已经满足了其所得糖果比两边大的条件。总结一下:正向遍历,无法满足数组首元素为降序的情况;反向遍历,若只考虑前者比后者大,不满足若某元素已经取得局部最大的情况。所以此时应该加一定的限制条件,如代码所示:

 class Solution {
public:
int candy(vector<int> &ratings)
{
int len=ratings.size();
int res=;
vector<int> cans(len,);
if(len<) return ; for(int i=;i<len-;++i)
{
if(ratings[i]<ratings[i+])
cans[i+]=cans[i]+;
}
for(int i=len-;i>;i--)
{
if(ratings[i]<ratings[i-]&&cans[i-]<=cans[i]) //限定
cans[i-]=cans[i]+;
} for(int i=;i<len;++i)
res+=cans[i]; return res;
}
};

还有一种空间复杂度为O(1)的解法,见GeekFans的博客,不过个人感觉思想类似,有兴趣可以看看。

[Leetcode] candy 糖果的更多相关文章

  1. [LeetCode] Candy (分糖果),时间复杂度O(n),空间复杂度为O(1),且只需遍历一次的实现

    [LeetCode] Candy (分糖果),时间复杂度O(n),空间复杂度为O(1),且只需遍历一次的实现 原题: There are N children standing in a line. ...

  2. [LeetCode] Candy 分糖果问题

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

  3. [LeetCode] Candy Crush 糖果消消乐

    This question is about implementing a basic elimination algorithm for Candy Crush. Given a 2D intege ...

  4. [Leetcode 135]糖果分配 Candy

    [题目] There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  5. leetcode — candy

    /** * Source : https://oj.leetcode.com/problems/candy/ * * There are N children standing in a line. ...

  6. Leetcode Candy

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

  7. [leetcode]Candy @ Python

    原题地址:https://oj.leetcode.com/problems/candy/ 题意: There are N children standing in a line. Each child ...

  8. LeetCode: Candy 解题报告

    Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  9. 算法题之Leetcode分糖果

    题目: There are N children standing in a line. Each child is assigned a rating value. You are giving c ...

随机推荐

  1. WinForm webbrowser控件的使用

    webbrowser是一个比较实用的工具,主要用于在winform窗体中嵌入浏览器,达到winform与webform互操作的目的. 先上一个demo,看一下能实现什么效果. private void ...

  2. pyc是个什么鬼?

    1.Python是一门解释型语音? 我初学Python时,听到的关于Python的第一句话就是,Python是一门解释型语音,我就这样一直相信下去,知道发现了*.pyc文件的存在.如果是解释型语音,那 ...

  3. Python3 利用pip安装BeautifulSoup4模块(Windows版)

    一.找到Python3的安装文件夹 二.将路径复制 三.Windows10 打开Windows PowerShell(管理员).Windows 8.8.1.7使用cmd 切换到相应目录 四.此目录下的 ...

  4. go学习笔记-流程控制(if/else,for/range)

    流程控制(if/else,for/range) if if条件判断语句的语法概括起来就是:如果满足条件就做某事,否则做另一件事. func testIf() { num := 10 if num &l ...

  5. dotnet core 数据库

    dotnet core 数据库 程序开发过程中,需要使用数据对数据进行存储,分析等.通常而言都会使用ORM来实现关系数据库与实体对象的转化,过使用描述对象和数据库之间映射的元数据,将程序中的对象自动持 ...

  6. 贪心算法之Prim

    Prim与Dijistra算法有异曲同工之妙,只不过Dijistra是求最短路径,每次添加到集合中的是到固定起始点的最短距离,而Prim是求最小生成树,是整个图所有权重的最小和,每次添加到集合中的是到 ...

  7. HDU暑假多校第八场G-Card Game

    一.题意 给出N个卡牌,卡牌的正反两面具有两个数字,取值范围为[1,2*n],给出若干个默认正面向上的卡牌,求最小反转多少张卡牌可以使得,每张卡牌朝上的面上都有一个不同的数字,同时满足最小反转次数的反 ...

  8. 查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分

    SELECT L.C# As 课程ID,L.score AS 最高分,R.score AS 最低分 FROM SC L ,SC AS R WHERE L.C# = R.C# and L.score = ...

  9. xss挑战赛小记 0x01(xsstest)

    0x00 今天在先知社区看到了一个xss挑战赛 结果发现比赛已经结束 服务器也关了 百度找了个xss挑战赛来玩一下 正好印证下xss的学习--- 地址     http://test.xss.tv/ ...

  10. 【转】Python 数据库连接池

    python编程中可以使用pymysql进行数据库连接及增删改查操作,但每次连接mysql请求时,都是独立的去请求访问,比较浪费资源,而且访问数量达到一定数量时,对mysql的性能会产生较大的影响.因 ...