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. Orcle基本语句(六)

    -- Created on 2017/1/5 by ADMINISTRATOR DECLARE -- Local variables here i ; v_name ) := '张晓风'; v_age ...

  2. 生产uuid

    uuid生产功能 近端时间要做一个获取唯一流水号的功能,于是有了:ip+starttime+pid+flow的方式. import java.lang.management.ManagementFac ...

  3. mysql快速导入大量数据问题

    今天需要把将近存有一千万条记录的*.sql导入到mysql中.使用navicate导入,在导入100万条之后速度就明显变慢了, 导入五百万条数据的时候用了14个小时,且后面的数据导入的越来越慢. 后来 ...

  4. iOS 数据序列化,NSCoding, NSCoder

    iOS可以利用NSKeyedArchiver类将对象序列化成NSData存储在磁盘上,但前提是该对象所属的类必须遵从NSCoding协议. NSCoding协议包含两个方法,要序列化的类必须实现它们 ...

  5. Python:关于字典的相关操作

    >>> people = {"Tom":170, "Jack":175, "Kite":160, "White& ...

  6. .NET Framework 框架的一些简单介绍

    20世纪90年代以来出现的3种典型的组件技术: 1)OMC(对象组件模型)的CORBA2)Microsoft的COM/DCOM3)Sun公司的JavaBeans 在2002年,微软发布了.NET框架的 ...

  7. 关于Comparable与Comparator

    首先说明一下这两个都是用来排序的, 只是应用起来有点差别而已. 有以下情景: 对于 List<String> list = new ArrayList<String>(); 我 ...

  8. vim中 set 用法设置

    vi set用法from google search一.常用收集如下:(vi set)set autoindent     在插入模式下,对每行按与上行同样的标准进行缩进,与shiftwidth选项结 ...

  9. jQuery MiniUI Demo

    http://www.miniui.com/demo/#src=datagrid/datagrid.html

  10. [专题论文阅读]【分布式DNN训练系统】 FireCaffe

    FireCaffe Forrest N. Iandola FireCaffe: near-linear acceleration of deep neural network training on ...