[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 ...
随机推荐
- java System.getProperty()参数大全
java.version Java Runtime Environment versionjava.vendor Java Runtime Environment vendorjava.vendor. ...
- 初学Python之异步多线程:sockserver
异步多线程:服务端 import socketserver class MyServer(socketserver.BaseRequestHandler): def setup(self): pass ...
- IP转换hash以及返回
InetAddress address = InetAddress.getByName("127.0.0.1"); System.out.println(address); int ...
- Nodejs学习(三)-安装nodejs supervisor,提高点效率吧。
安装好了express准备写项目,可是发现随便改一下js都要使用npm start重新启动才能生效,这个很不好,搜索一下发现有这么一个模块supervisor.那就安装一下吧. 1.安装,这个必须是全 ...
- Tomcat安装后,远程IP无法访问的问题。
我在使用阿里云与聚石塔的时候,发现Tomcat启动后,本地可以访问,但是外网无法访问,即使关闭防火墙也无法访问. 原因是 云平台的网络拦截. 阿里云:有一个入网规则 和 出网规则 ,流入数据端口 流 ...
- ffmpeg基础
背景知识ffmpeg是一款领先的流媒体处理框架,支持编码,解码,转码等功能并可以在linux, Mac OS X, Microsoft Windows编译运行,用它做播放器的有:ffplay,射手播放 ...
- AIX 环境下减小系统page space空间
IBM AIX v5.3操作系统环境下减小系统page space空间详细步骤如下 1,创建一个临时的page space空间#mkps -a -n -s 20 rootvg 这里-a参数指定页面空间 ...
- Loadrunner开发测试脚本
Loadrunner开发测试脚本 开发测试脚本可以通过录制,也可以手动开发,建议能录制的尽量录制,省时省力,不能录制的只能费力自己开发了,具体看项目情况来决定. 使用Loadrunner开发脚本过程中 ...
- 情感分析的现代方法(包含word2vec Doc2Vec)
英文原文地址:https://districtdatalabs.silvrback.com/modern-methods-for-sentiment-analysis 转载文章地址:http://da ...
- 删除ubuntu后无法进入windows
一天,脑袋一热想装个ubuntu学习学习,结果装的ubuntu版本比较老,没有集成我笔记本的网卡驱动,在ubuntu下上不了网,心想上不了网学习起来不是很不方便,于是就直接在win7下将ubuntu的 ...