Candy leetcode java
题目:
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?
题解:
这道题和Trapping water那个是一样的想法,因为无论是水坑还是得到糖的小朋友,影响因素都不只一边,都是左右两边的最小值/最大值来决定的。
所以这道题跟上一道一样,也是左右两边遍历数组。
leftnums数组存从左边遍历,当前小朋友对比其左边小朋友,他能拿到糖的数量;
rightnums数组存从右边遍历,当前小朋友对比其右边小朋友,他能拿到的糖的数量。
最后针对这两个数组,每个小朋友能拿到的糖的数量就是这两个数最大的那个数,求总加和就好了。
代码如下:
1 public int candy(int[] ratings) {
2 if(ratings==null || ratings.length==0)
3 return 0;
4
5 int[] leftnums = new int[ratings.length];
6 int[] rightnums = new int[ratings.length];
7
8 leftnums[0]=1;
9 for(int i=1;i<ratings.length;i++){
if(ratings[i]>ratings[i-1])
leftnums[i] = leftnums[i-1]+1;
else
leftnums[i] = 1;
}
rightnums[ratings.length-1] = leftnums[ratings.length-1];
for(int i=ratings.length-2;i>=0;i--){
if(ratings[i]>ratings[i+1])
rightnums[i] = rightnums[i+1]+1;
else
rightnums[i] = 1;
}
int res = 0;
for(int i = 0; i<ratings.length; i++)
res += Math.max(leftnums[i],rightnums[i]);
return res;
}
Candy leetcode java的更多相关文章
- N-Queens II leetcode java
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...
- [LeetCode][Java]Candy@LeetCode
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- Regular Expression Matching leetcode java
题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...
- Sqrt(int x) leetcode java
Reference: http://blog.csdn.net/lbyxiafei/article/details/9375735 题目: Implement int sqrt(int x). Co ...
- ZigZag Conversion leetcode java
题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...
- [Leetcode][JAVA] Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- *candy——leetcode
/* */ #include<iostream> #include<vector> //#include<algorithm> #include <windo ...
- 【目录】LeetCode Java实现
这里记录一下自己刷的LeetCode题目. 有些博客用英文阐述自己的思路和收获,相当于练习一下英文的表达能力. 比较好的题目有加粗. 1. Two Sum 3. Longest Substring W ...
- Single Number II leetcode java
问题描述: Given an array of integers, every element appears three times except for one. Find that single ...
随机推荐
- python3.6.5中pip3无法使用
1.在python命令行窗口中: python3 -m ensurepip 创建出pip3.exe.2.再在python3.6的安装目录下的Scripts路径下命令行 pip3 install XXX ...
- Spring boot之SpringApplicationBuilder,@@Configuration注解,@Component注解
SpringApplicationBuilder: 该方法的作用是可以把项目打包成war包 需要配置启动类,pom.xml文件等,具体见:http://blog.csdn.net/linzhiqian ...
- (视频)asp.net core系列之k8s集群部署视频
0.前言 应许多网友的要求,特此录制一下k8s集群部署的视频.在录制完成后发现视频的声音存在一点瑕疵,不过不影响大家的观感. 一.视频说明 1.视频地址: 如果有不懂,或者有疑问的欢迎留言.视频分为两 ...
- 解释一下什么是servlet?
Servlet是一种独立于平台和协议的服务器端的Java技术,可以用来生成动态的Web页面.与传统的CGI(计算机图形接口)和许多其他类似CGI技术相比,Servlet具有更好的可移植性.更强大的功能 ...
- IE访问历史记录恢复工具pasco
IE访问历史记录恢复工具pasco IE浏览器会自动记录用户访问网站的操作,并将这些信息保存在IE缓存中的index.dat文件中.通过分析这些历史记录,可以还原用户行为.为了便于数字取证,Kali ...
- 家谱(gen)
家谱(gen) 时间限制 2S [问题描述] 现代的人对于本家族血统越来越感兴趣,现在给出充足的父子关系,请你编写程序找到某个人的最早的祖先. [输入格式]gen.in 输入文件由多行组成, ...
- luoguP3480 [POI2009]KAM-Pebbles 阶梯Nim
将序列差分并翻转之后,变成了阶梯\(Nim\)的模板题 QAQ #include <cstdio> #include <cstring> #include <iostre ...
- python开发_tkinter_获取单选菜单值
在之前的blog中有提到python的tkinter中的菜单操作 python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推荐 python开发_tkinter_窗口控件_自 ...
- Codeforces Round #259 (Div. 1) A. Little Pony and Expected Maximum 数学公式结论找规律水题
A. Little Pony and Expected Maximum Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.c ...
- hdoj 5182 PM2.5 排序
PM2.5 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem Descr ...