[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 ...
随机推荐
- StrangeIoc框架学习----在项目中实战
最近,因为公司的项目一直在研究StrangeIoc框架,小有所得,略作记录. StrangeIoc是一款基于MVCS的一种框架,是对MVC思想的扩展,是专门针对unity开发的一款框架,非常好用. 一 ...
- Mybatis分页插件
mybatis配置 <!-- mybatis分页插件 --> <bean id="pagehelper" class="com.github.pageh ...
- Delphi JCL JEDI使用 jclDebug
开源 https://github.com/project-jedi/jcl jclDebug 下载jcl,还要下载https://github.com/project-jedi/jedi里的2个in ...
- xgboost原理及应用
1.背景 关于xgboost的原理网络上的资源很少,大多数还停留在应用层面,本文通过学习陈天奇博士的PPT 地址和xgboost导读和实战 地址,希望对xgboost原理进行深入理解. 2.xgboo ...
- 前端---DOM
一.介绍: http://www.cnblogs.com/wupeiqi/articles/5643298.html 什么是DOM? DOM字面意思是文档对象模型,DOM将网页内的元素与内容呈现为一个 ...
- ASP.NET Misconfiguration: Missing Error Handling
Abstract: An ASP .NET application must enable custom error pages in order to prevent attackers from ...
- Res_Orders_02
一.燃尽图展示 二.项目进展 1.实现用户名找回 2.css样式嵌入
- 如何在SqlServer中获取前端连接的IP地址,计算机名等信息
在一些需求中,可能我们需要知道连接到SqlServer的前端程序的一些系统信息,比如前端连接的计算机名称,IP地址,什么时候开始请求连接,什么时候结束连接等信息. 如果你对SqlServer的系统函数 ...
- 什么是JVM?
什么是JVM? JVM是Java Virtual Machine(Java虚拟机)的缩写,JVM是一种用于计算设备的规范,它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟各种计算机功能来实现的 ...
- 【转】Web前端研发工程师编程能力飞升之路
分类: Javascript | 出自 海玉的博客 今天看到这篇文章.写的非常有意思.发现自己还有很长的一段路要走. [背景] 如果你是刚进入WEB前端研发领域,想试试这潭水有多深,看这篇文章吧: 如 ...