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. ApexSQLLog总结

    我们程序最近做更新,要做新功能的测试.程序经常出现大的事务,每次commit之后如果发现数据不对的问题也不能再回滚进行断点调试了,因为数据库数据已经更改了,所以一直想找一个回滚我数据库所有操作的工具. ...

  2. cx_Oracle 中文乱码问题解决

    设置NLS_LANG环境变量 import os os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'

  3. MySQL 5.7 Command Line Client输入密码后闪退和windows下mysql忘记root密码的解决办法

    MySQL 5.7 Command Line Client输入密码后闪退的问题: 问题分析: 1.查看mysql command line client默认执行的一些参数.方法:开始->所有程序 ...

  4. HTML5来回拖动实例

    <html> <meta charset="utf-8"> <script> //规定被拖动的数据 function tdwhat(ev,obj ...

  5. maven增加自定义jar包

    1.博客来源: http://www.cnblogs.com/leiOOlei/p/3356834.html 导入平台SDK的方法 mvn install:install-file -DgroupId ...

  6. SpringMVC学习笔记(六)

    一.SpringMVC文件的上传 1.1.需要导入两个jar包 1.2在SpringMVC配置文件中加入 <!-- upload settings --> <bean id=&quo ...

  7. C# ASP.NET(配置数据库 sql server 地址的两种形式以及配置信息的获取)

    ( 1 ) 数据库装在本机,并且采用windows认证模式 <connectionStrings>    <add name="SQLConnectionString&qu ...

  8. Roguelike 相关知识

    here is the link here

  9. Portable Basemap Server:多数据源多客户端的底图服务器

    Portable Basemap Server:多数据源多客户端的底图服务器 [poll id=”1″]2014.3.8更新v3.1~在线切片转换为MBTiles时,增加RecreateEmptyCa ...

  10. VC++中StretchBlt图像失真问题的解决办法

    在 VC 中使用 StretchBlt 会碰到一些与点阵图大小缩放相关的一些问题.在扩展一个点阵图时,StretchBlt必须复制图素行或列.如果放大倍数不是原图的整数倍,那么此操作会造成产生的图像有 ...