[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 ...
随机推荐
- TFS 2010 迁移/重装/还原 步骤
1.签入所有代码 2.停止TFS服务:运行命令行,并将路径切换到TFS安装路径:C:\Program Files\Microsoft Team Foundation Server 2010\Tools ...
- PS1应用之——修改linux终端命令行各字体颜色
最近在学习linux操作系统(CentOS 6 & CentOS 7).觉得linux终端命令行全部为白色,会经常导致命令与输出内容难以分辨.于是上网找到修改linux终端命令行颜色的方法,发 ...
- Only MySqlParameter objects may be stored
Only MySqlParameter objects may be stored 今天碰到了这个问题琢磨了半天,最后发现是MySql.Data.dll版本问题,换了个最新版本的就可以了.
- 在线c++编译器(gcc)
这几年c++标准委员会活跃起来,C++11.14标准相续推出.对于想尝鲜又怕麻烦(visual studio 更新慢,对标准的支持力度也不够.对于使用gcc的,替换系统的gcc版本或者安装个mingw ...
- 后台js弹提示
StringBuffer sb=new StringBuffer(); try{ sb.append("<script> location.href=\"member_ ...
- 第二次C语言作业
实验一:判断成绩等级. 给定一百分制成绩,要求输出成绩的等级.90以上为A,80-89为B,70-79为C,60-69为D,60分以下为E,输入大于100或小于0时输出"输入数据错误&quo ...
- jquery仿淘宝规格颜色选择效果
jquery实现的仿淘宝规格颜色选择效果源代码如下 jquery仿淘宝规格颜色选择效果 -收缩HTML代码 运行代码 [如果运行无效果,请自行将源代码保存为html文件运行] <script t ...
- spring注解记录
集中记录spring常见注解 供今后查阅 @ControllerAdvice: ControllerAdvice的定义为: @Target(ElementType.TYPE) @Retention(R ...
- 查询数据库最大id加1
SELECT ISNULL(MAX(id),0)+1 AS MaxId FROM TABLE ISNULL(MAX(id),0) 就是如果id为空 就返回0,然后再加1
- php中Content-type说明
'hqx' -> 'application/mac-binhex40','cpt' -> 'application/mac-compactpro','doc' -> 'applica ...