Candy

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?

Runtime: 392 ms

这道题跟Leetcode 一道contain water的题很像,思想很简单,可以把每个小朋友单独考虑,看下每个小朋友左边有紧邻的小朋友rate降序的个数,再看下该小朋友右边紧邻的小朋友降序的个数,然后就能确定本小朋友的最小“高度”(两降序个数中较大值),即为小朋友应得的糖果。

方法:从前往后,从后往前扫描两遍,并用lowleft[],lowright[]分别记录每个小朋友两边的降序个数即可~

public class Solution {

public int candy(int[] ratings) {

if(ratings.length==0) return 0;

if(ratings.length==1) return 1;

int[] lowleft=new int[ratings.length];

int[] lowleft=new int[ratings.length];

int ret=0;

lowleft[0]=0;

for(int i=1;i<ratings.length;i++){

if(ratings[i-1]<ratings[i]) lowleft[i]=lowleft[i-1]+1;

//else if(ratings[i-1]==ratings[i]) lowleft[i]=lowleft[i-1];

else lowleft[i]=0;

}

lowright[ratings.length-1]=0;

for(int j=ratings.length-2;j>=0;j--){

if(ratings[j+1]<ratings[j]) lowright[j]=lowright[j+1]+1;

//else if(ratings[j+1]==ratings[j]) lowright[j]=lowleft[j+1];

else lowright[j]=0;

}

for(int i=0;i<ratings.length;i++){

ret=ret+1+Math.max(lowright[i],lowleft[i]);

}

return ret;

}

}

[LeetCode][Java]Candy@LeetCode的更多相关文章

  1. [LeetCode][Java]Triangle@LeetCode

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  2. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  3. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  4. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  5. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  6. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  7. Java for LeetCode 200 Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  8. Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  9. Java for LeetCode 154 Find Minimum in Rotated Sorted Array II

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

随机推荐

  1. qsort库函数的用法

    qsort 功 能: 使用快速排序例程进行排序  用 法: void qsort(void *base, int nelem, int width, int (*fcmp)(const void *, ...

  2. 1、Python环境安装部署

    一.环境准备 1.下载Python安装包(至官方网站) https://www.python.org/downloads/ 建议下载安装最新版 2.设置"环境变量" "我 ...

  3. 动态设置和访问cxgrid列的Properties(转)

    原文:http://www.cnblogs.com/hnxxcxg/archive/2010/05/24/2940711.html 动态设置和访问cxgrid列的Properties 设置: cxGr ...

  4. MySQL事件 Events

    MySQL事件 Events   一.关键字: EVENT   二.语法: CREATE EVENT? [IF NOT EXISTS ] //如果不存在则创建 event_name? ON SCHED ...

  5. js计时事件

    通过在一个设定的时间间隔之后来执行代码,而不是在函数被调用后立即执行,我们称之为计时事件. 1. setTimeout()--暂停指定的时间后执行指定的代码 clearTimeout ()--停止se ...

  6. Ubuntu 命令大全

    一.文件目录类 1.建立目录:mkdir 目录名 2.删除空目录:rmdir 目录名 3.无条件删除子目录: rm -rf 目录名 4.改变当前目录:cd 目录名 (进入用户home目录:cd ~:进 ...

  7. 平板上的js和电脑上js的不同之处

    一.事件 1.平板上没有:onmousedown,onmouseup,onmousemove等事件,由ontouchstart,ontouchmove,ontounchend替代 2.位置问题:平板上 ...

  8. webstorm 2016 激活(转)

    2016.2.2 版本的破解方式: 安装以后,打开软件会弹出一个对话框:选择"license server" 输入:http://114.215.133.70:41017 2016 ...

  9. 信鸽推送(XGPush)

    先放入两个链接: iOS信鸽接入官方文档:http://developer.qq.com/wiki/xg/iOS接入/iOS%20SDK完整接入/iOS%20SDK完整接入.html 信鸽开放平台:h ...

  10. mix_alternates_for_parent: TRUE

    主替代料的消耗问题就没有了mix_alternates_for_parent: TRUEmix_alternate_per_unit_parent: TRUE D:\JDA\JDAv800\FP\in ...