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?

思路:neighbor=>元素值与前、后元素有关=>

第一次前向扫描:保证high rate元素比左邻居糖多;

第二次后向扫描:保证high rate元素比右邻居糖多;

class Solution {
public:
int candy(vector<int> &ratings) {
vector<int> new_ratings(ratings.size(), );
for(int i = ; i < ratings.size(); i++) //for the first time, scan from beginning
{
if(ratings[i] > ratings[i-])
{
new_ratings[i] = new_ratings[i-]+;
} }
for(int i=ratings.size()-;i>=;i--){ //for the second time, scan from the end
if(ratings[i]>ratings[i+]&&new_ratings[i]<=new_ratings[i+]){
new_ratings[i]=new_ratings[i+]+;
}
}
int sum=;
for(int i=;i<new_ratings.size();i++){
sum+=new_ratings[i];
} return sum;
}
};

135. Candy(Array; Greedy)的更多相关文章

  1. LeetCode 135 Candy(贪心算法)

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

  2. leetcode 135. Candy ----- java

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

  3. Leetcode#135 Candy

    原题地址 遍历所有小孩的分数 1. 若小孩的分数递增,分给小孩的糖果依次+12. 若小孩的分数递减,分给小孩的糖果依次-13. 若小孩的分数相等,分给小孩的糖果设为1 当递减序列结束时,如果少分了糖果 ...

  4. (LeetCode 135) Candy N个孩子站成一排,给每个人设定一个权重

    原文:http://www.cnblogs.com/AndyJee/p/4483043.html There are N children standing in a line. Each child ...

  5. 135. Candy

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

  6. [leet code 135]candy

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

  7. 55. Jump Game (Array; Greedy)

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  8. 【LeetCode】135. Candy

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

  9. Java for LeetCode 135 Candy

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

随机推荐

  1. unity导入3dsMax源文件.max

    https://blog.csdn.net/qq_28002559/article/details/53693621 首先unity导入3dsMax文件为什么要使用.max而不用.fbx,因为我不是做 ...

  2. JDK1.5java新特性

    JDK1.5java增加的新特性: 自动装箱/拆箱      增强for     泛型      枚举      静态导入      可变参数 1 自动装箱/拆箱 * JDK1.5允许开发人员把一个基 ...

  3. Iterations --codility

    lesson 1:Iterations 1. BinaryGap-----[100%] Find longest sequence of zeros in binary representation ...

  4. angualrJS(mvc)指令嵌套使用的一些问题

    angular的指令拥有一个独立作用域的概念. 一般定义指令的形式: define(['app'],function(mianapp){ mainapp.directive("tlmsAol ...

  5. Nginx RTMP 模块 nginx-rtmp-module 指令详解

    译序:截至 Jul 8th,2013 官方公布的最新 Nginx RTMP 模块 nginx-rtmp-module 指令详解.指令Corertmp语法:rtmp { ... }上下文:根描述:保存所 ...

  6. bzoj1729: [Usaco2005 dec]Cow Patterns 牛的模式匹配

    Description     约翰的N(1≤N≤100000)只奶牛中出现了K(1≤K≤25000)只爱惹麻烦的坏蛋.奶牛们按一定的顺序排队的时候,这些坏蛋总会站在一起.为了找出这些坏蛋,约翰让他的 ...

  7. git 绑定远程仓方法

    第1步:创建SSH Key.在用户主目录下,看看有没有.ssh目录,如果有,再看看这个目录下有没有id_rsa和id_rsa.pub这两个文件,如果已经有了,可直接跳到下一步.如果没有,打开Shell ...

  8. DOM的基本概念

    1.DOM的基本概念 DOM是文档对象模型,这种模型为树模型:文档是指标签文档:对象是指文档中每个元素:模型是指抽象化的东西. 2.Window对象操作 一.属性和方法: 属性(值或者子对象): op ...

  9. 在虚拟机里新建一个20G的硬盘,如何把他挂载在 /work 目录上

    目的:在虚拟机里新建一个20G的硬盘,然后想把他挂载在 /work 目录上 /dev/sda (系统盘) /dev/sdb (数据盘) /dev/sdc (数据盘) /dev/sdd (数据盘) /d ...

  10. JoinableQueue

    #!/usr/bin/env python # encoding: utf-8  # Date: 2018/6/17import timefrom multiprocessing import Pro ...