[LeetCode][Java]Candy@LeetCode
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的更多相关文章
- [LeetCode][Java]Triangle@LeetCode
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- 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 ...
- 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. ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- python的optparse模块使用
name or flags:就是参数的名称或标志 -f --file,-q --quit 等,其中-f表示option的缩写,--file表示option的全称 nargs:命令行参数的个数,一般使用 ...
- Django URL的命令空间
为避免在模板中使用URL的硬编码,可以使用{% url %}模板标签来解决 <li><a href="/task/{{ task.id }}/">{{ ta ...
- thinkphp修改及编写标签库,编辑器的使用
在view目录下创建Index_aaa.html <!DOCTYPE html> <html> <head> <meta charset="UTF- ...
- 关于asp.net 开发的小技巧—让传值对象化
前端:前端 定义一个对象, 传值时实例此对象,序列化成json字符串 代码如下: 定义js对象: ///定义一个查询条件对象 var SearchCondition=function(){ this. ...
- 提取数据库字段里面的值,并改变+图片懒加载,jquery延迟加载
要求:手机端打开某个页面的详细信息,因为网速或者别的原因,响应太慢,因为图片大的原因,希望先进来,图片在网页运行的情况再慢慢加载(jquer延迟加载) http://www.w3cways.com/1 ...
- 线程+IO流
第十八天知识点总结 线程的停止: 1.停止一个线程,一般是通过一个变量来控制. 2.如果需要停止一个处于一个等待状态的线程,那么需要配合interrupt方法来完成 守护线程(后台线程):在一个进程中 ...
- XidianOJ 1041: Franky的游戏O
题目描述 Franky是super的人造人,来到了n*m的棋盘世界玩冒险游戏. n×m的棋盘由n行每行m个方格组成,左上角的方格坐标是(0,0),右下角的方格坐标是(n-1,m-1). 每次游戏时,他 ...
- php获取时间问题,用默认配置读到本地时间。。。。。
用date获取到时间有8小时时差 因为php用date获取到的是格林威治时区的时间,而大陆时间和格林威治时间有8个小时时差,所以.... 修改:网上有各种修改方式: 比如:在程序中添加时间的初始化的语 ...
- 多层数据库应用基于Delphi DataSnap方法调用的实现(一)返回数据集
从Delphi 2009开始,DataSnap技术发生了很大的变化,并在Delphi 2010和Delphi XE的后续版本中得到了持续的改进.Delphi 2009之前的DataSnap,虽然也实现 ...
- ViewPager如下效果你研究过吗?
1:ViewPager实现欢迎页面动画效果 ViewPager实现欢迎页面动画滑动切换view效果,页面切换添加优美的动画, //主要代码实现 public void animateSecondScr ...