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?

题目的意思:

  有N个孩子站成一条线,每个孩子分配一个排名。你将要给这些孩子一些糖果,要求:

  • 每个孩子必须至少有一个糖果
  • 孩子如果有个高排名,将得到糖果比其邻居的多

解题思路是:

  (1)由于每个小孩至少有一个糖果,故将每个小孩的初始糖果初始化为1

  (2)从前往后扫描,如果第i个小孩排名比第i-1高,那么第i个小孩的糖果数目+1

  (3)从后往前扫描,如果第i个小孩排名比第i+1高,那么第i个小孩的糖果数目=max(第i个小孩的糖果数目,第i+1个小孩的糖果数目+1)

  (4)最后将所有小孩的糖果数目累积即可

class Solution {
public:
int candy(vector<int> &ratings) {
vector<int> candy(ratings.size(),);
for(int i = ; i < ratings.size(); ++ i){
if(ratings[i]>ratings[i-]) candy[i]=candy[i-]+;
}
for(int i = ratings.size()-; i>=; -- i){
if(ratings[i] > ratings[i+]) candy[i] = max(candy[i+]+,candy[i]);
}
return accumulate(candy.begin(),candy.end(),);
}
};

关于此题目类似的题目

  在一维数组中,找出一个点,使得其所有左边的数字均小于等于它,所有右边的数字都大于等于它。要求在线性时间内返回这个点所在的下标。

如A={1,0,1,0,1,2,3},返回下标4或5

解题思路与上面类似

  首先,从左到右扫描一遍数组,通过一个辅助布尔数组记录哪些大于等于其之前所有元素的元素;

  其次,从右到左扫描一遍数组,如果其后所有元素大于等于当前元素,而且在第一个遍历时当前元素大于等于之前的所有元素,则程序返回下标;

int getMagnitutePole(vector<int> A){
if(A.size() == ) return ;
vector<bool> flag(A.size(), false);
int curMax = A[];
for(int i = ; i < A.size(); ++ i){
if(A[i]>=curMax){
curMax = A[i];
flag[i] = true;
}
}
int curMin = A[A.size()-];
for(int i = A.size()-; i >=; -- i ){
if(A[i] <= curMin){
curMin = A[i];
if(flag[i]) return i;
}
}
return -;
}

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 — candy

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

  5. [leetcode]Candy @ Python

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

  6. LeetCode: Candy 解题报告

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

  7. [Leetcode] candy 糖果

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

  8. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  9. Solution to LeetCode Problem Set

    Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...

随机推荐

  1. ios几个重要方法

     加载类到内存,程序刚启动的时候调用,调用在main函数之前 1.+(void)load{ } 初始化类,类第一次使用的时候调用一次 2.+(void)initialize{ } 控制器的视图架构,设 ...

  2. 封装自己的Common.js工具库

    Code/** * Created by LT on 2013/6/16. * Common.js * 对原生JS对象的扩展 * Object.Array.String.Date.Ajax.Cooki ...

  3. CentOS6.3 编译安装LAMP(1):准备工作

    卸载yum或rpm安装的amp软件 #在编译安装lamp之前,首先先卸载已存在的rpm包. rpm -e httpd rpm -e mysql rpm -e php yum -y remove htt ...

  4. H5案例分享:移动端滑屏 touch事件

    移动端滑屏 touch事件 移动端触屏滑动的效果的效果在电子设备上已经被应用的越来越广泛,类似于PC端的图片轮播,但是在移动设备上,要实现这种轮播的效果,就需要用到核心的touch事件.处理touch ...

  5. js parseInt 显示0

    parseInt 有第二个参数, 就是进制参数 parseInt("08", 10);  //表示这个数字是十进制的就不会出错了.

  6. UIStackView

    既要温故又要知新...温故而知新,我懂. 在Cocoachina上看到了,UIStackView. 没用过额,试iOS9的新特性.一研究,妈的还挺眼熟.一想,这不和Android上的线性布局一样么.. ...

  7. CTR预估评价指标介绍

    1 离线指标 1.1 LogLoss 1.1.1 KL散度 logloss使用KL散度来计算.设样本的真实分布为P,预测分布为Q,则KL散度定义如下: 这里可以通俗地把KL散度理解为相同事件空间里两个 ...

  8. iframe使用方法

    --点击按钮会把地址里的页面显示在oframe里,对iframe可以设置宽和高<iframe src="demo_iframe.htm" name="iframe_ ...

  9. git 入门操作

    从已有的GIT仓库获取最新代码 git clone url 建立本地仓库,并提交至git-hub 生成ssh的公钥私钥对:ssh-keygen 必须把这两个文件放到当前用户目录的“.ssh”目录下才能 ...

  10. CSS3 外发光 渐变色

    渐变色--线性渐变 linear-gradient #bg { width: 100%; background: -webkit-linear-gradient(#9f1c65, #d89068,#7 ...