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. Recall, Precision and F-score

    F1 score (also F-score or F-measure) ,调和平均数稍微有点不好理解,最关键的是,不知道分子的情况下,采用调和平均数.

  2. thinkphp 导出exl功能

    /** * 导出数据为excel表格 *@param $data 一个二维数组,结构如同从数据库查出来的数组 *@param $title excel的第一行标题,一个数组,如果为空则没有标题 *@p ...

  3. Atom编辑器的插件

    先说下atom的插件安装方法吧,因为没用过苹果电脑,所以这里就只说下windows的操作吧. " ctrl+, "调出设置面板 点击install按钮,进去搜索插件面板 1.exp ...

  4. yii2 伪静态配置

    原文地址: http://gblz.net/2015/242.html https://segmentfault.com/q/1010000003804408

  5. 基本 sql语句

    1.打开数据库 int sqlite3_open( const char *filename,   // 数据库的文件路径 sqlite3 **ppDb          // 数据库实例 ); 2. ...

  6. 【转】搞不清FastCgi与php-fpm之间是个什么样的关系?

    我在网上查fastcgi与php-fpm的关系,查了快一周了,基本看了个遍,真是众说纷纭,没一个权威性的定义. 网上有的说,fastcgi是一个协议,php-fpm实现了这个协议: 有的说,php-f ...

  7. jquery和dom之间的转换

    刚开始学习jquery,可能一时会分不清楚哪些是jQuery对象,哪些是DOM对象.至于DOM对象不多解释,我们接触的太多了,下面重点介绍一下jQuery,以及两者相互间的转换. 什么是jQuery对 ...

  8. poj 1170

    很高兴,这道题刚编译成功提交就AC了. 简单的多重背包,标算估计是5.6维动规.其实可以通过6进制压成一维.判定是一个特价方式是否可行只需自己推一下就行了,很简单(对应位上的数目标不小于特价所需条件) ...

  9. iterator接口

    Iterator用来做遍历,所有实现Collection接口的容器都有一个Iterator的方法以返回一个Iterator接口的对象

  10. python基础三

    多级菜单 多级菜单 可依次选择进入各子菜单 所需新知识点:列表.字典 #!/usr/bin/env python # -*- coding: utf-8 -*- menu = { '北京':{ '海淀 ...