leetcode — candy
/**
* Source : https://oj.leetcode.com/problems/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?
*
*/
public class Candy {
/**
* 给站成一排的孩子分糖果,
* 每个孩子至少一个
* 每个孩子的权重不一样,权重高的孩子比相邻的孩子要多
* 求最少需要多少个糖果
*
* 从左到右遍历,寻找递增序列,该位置对应的孩子分的糖果数也递增,每个递增序列从1开始,这样保证了rating高的孩子比左边的孩子多
* 然后从右向左遍历,确保当前位置孩子的糖果数多于右边孩子的
* 复杂度为O(n)
*
* @param ratings
* @return
*/
public int candy (int[] ratings) {
if (ratings.length <= 0) {
return 0;
}
int[] candy = new int[ratings.length];
candy[0] = 1;
for (int i = 1; i < ratings.length; i++) {
if (ratings[i] > ratings[i-1]) {
candy[i] = candy[i-1] + 1;
} else {
candy[i] = 1;
}
}
int sum = candy[ratings.length-1];
for (int i = ratings.length-2; i > -1; i--) {
if (ratings[i] > ratings[i+1] && candy[i] <= candy[i+1]) {
candy[i] = candy[i+1] + 1;
}
sum += candy[i];
}
return sum;
}
public static void main(String[] args) {
Candy candy = new Candy();
int[] ratings = new int[]{5, 6, 7, 4, 1, 2, 3, 2, 1, 7};
System.out.println(candy.candy(ratings) + " == 19");
}
}
leetcode — candy的更多相关文章
- [LeetCode] Candy (分糖果),时间复杂度O(n),空间复杂度为O(1),且只需遍历一次的实现
[LeetCode] Candy (分糖果),时间复杂度O(n),空间复杂度为O(1),且只需遍历一次的实现 原题: There are N children standing in a line. ...
- [LeetCode] Candy 分糖果问题
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- [LeetCode] Candy Crush 糖果消消乐
This question is about implementing a basic elimination algorithm for Candy Crush. Given a 2D intege ...
- [leetcode]Candy @ Python
原题地址:https://oj.leetcode.com/problems/candy/ 题意: There are N children standing in a line. Each child ...
- Leetcode Candy
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- LeetCode: Candy 解题报告
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- [Leetcode] candy 糖果
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- Solution to LeetCode Problem Set
Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...
随机推荐
- echarts、higncharts折线图或柱状图显示数据为0的点
echarts.higncharts折线图或柱状图只需要后端传到前端一段json数据,接送数据的x轴与y周有对应数据,折线图或柱状图就会渲染出这数据. 比如,x轴表示美每天日期,y轴表示数量.他们的数 ...
- windows下 mysql 5.6.40 卸载 安装 修改密码
最近执行另一个mysql版本导出的sql脚本,出现问题!出于一些原因,把之前的mysql5.5卸载,由于卸载不干净出现了一些问题.特此总结方法! 参考链接: https://blog.csdn.net ...
- NPOI 修改指定单元格字体颜色
//创建一个字体颜色 IFont font = hssfworkbook.CreateFont(); //红色 font.Color = HSSFColor.Red.Index; //样式 ICell ...
- CSS背景图片
1.背景图片插入 代码格式:background-image:url(): 括号内填写图片路径 2.背景图片设置大小 代码格式:background-size:宽.高 3.背景图片设置不平铺 代码格式 ...
- tomcat +jenkios
来源:https://www.cnblogs.com/edward2013/p/5269465.html 1. 安装JDK JDK下载地址: http://www.oracle.com/techne ...
- touchweb网站常见问题,手机网站注意问题
一.h5网站input 设置为type=number的问题 h5网页input 的type设置为number一般会产生三个问题,一个问题是maxlength属性不好用了.另外一个是form提交的时候, ...
- TCP的三次握手过程与四次挥手
TCP握手协议 在TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建立一个连接.第一次握手:建立连接时,客户端发送syn包(syn=j)到服务器,并进入SYN_SEND状态,等待服务器确 ...
- Python爬虫7-Cookie & Session
GitHub代码练习地址:1.手动利用cookie访问网页:https://github.com/Neo-ML/PythonPractice/blob/master/SpiderPrac10_cook ...
- MyEclipse设置Console输出到文件
Java程序默认输出为Console,如果要想将Console输出结果保存到文件中,则需要做如下配置: 在JAVA程序上右键--> Run As --> Run Configuration ...
- [Swift]LeetCode940. 不同的子序列 II | Distinct Subsequences II
Given a string S, count the number of distinct, non-empty subsequences of S . Since the result may b ...