leetcode 135. Candy ----- 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?
从后向前判断:
1、找出从小到大排列的子序列,大小为n,分配的糖果应该是1、2、3....n
2、如果 n == 1 那么就应该是比之前子序列的开头要大,即 : 如果之前子序列中数字个数大于1,那么该位置分配2个糖果(应该之后一个位置的孩子得到了1个糖果),否则得到比后一个位置的孩子糖果多1 的糖果。
3、比较麻烦的是如果遇到相同rating的孩子:相同rating 的孩子可以得到不一样的糖果。
因此每次结束的时候需要判断当前子序列结尾的数字是否与下一个子序列开始的位置的数字相同,如果不相同,需要当前子序列结尾得到的数目大于在一个子序列开始的数目;如果一样,那么可以按照之前的顺序分配,即1、2、3、4...n
public class Solution {
public int candy(int[] ratings) {
int len = ratings.length;
int result = 0;
int pos = len-1;
int flag = 0,target = 0;
while( pos >= 0){
int size = 1;
while( pos >= 1 && ratings[pos-1] < ratings[pos] ){
size++;
pos--;
target = ratings[pos];
}
if( size > 1){
result+=(1+size)*size/2;
if(size <= flag ){
if( pos+size<len && ratings[pos+size] != ratings[pos-1+size])
result+=(flag-size+1);
}
flag = 1;
}
else {
if( pos<len-1 && ratings[pos] == ratings[pos+1]){
flag = 1;
result+=1;
}
else{
result += flag+1;
flag++;
}
}
pos--;
}
return result;
}
}
leetcode 135. Candy ----- java的更多相关文章
- LeetCode 135 Candy(贪心算法)
135. Candy There are N children standing in a line. Each child is assigned a rating value. You are g ...
- Java for LeetCode 135 Candy
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- (LeetCode 135) Candy N个孩子站成一排,给每个人设定一个权重
原文:http://www.cnblogs.com/AndyJee/p/4483043.html There are N children standing in a line. Each child ...
- Leetcode#135 Candy
原题地址 遍历所有小孩的分数 1. 若小孩的分数递增,分给小孩的糖果依次+12. 若小孩的分数递减,分给小孩的糖果依次-13. 若小孩的分数相等,分给小孩的糖果设为1 当递减序列结束时,如果少分了糖果 ...
- [leetcode] 135. Candy (hard)
原题 前后两遍遍历 class Solution { public: int candy(vector<int> &ratings) { vector<int> res ...
- Java实现 LeetCode 135 分发糖果
135. 分发糖果 老师想给孩子们分发糖果,有 N 个孩子站成了一条直线,老师会根据每个孩子的表现,预先给他们评分. 你需要按照以下要求,帮助老师给这些孩子分发糖果: 每个孩子至少分配到 1 个糖果. ...
- [Leetcode 135]糖果分配 Candy
[题目] There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- 【LeetCode】135. Candy
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- 135. Candy
题目: There are N children standing in a line. Each child is assigned a rating value. You are giving c ...
随机推荐
- BCP批量导入数据时候如何处理表中自动增加的字段
大容量导入数据时保留标识值 (SQL Server) http://msdn.microsoft.com/zh-cn/library/ms186335(v=sql.120).aspx 使用格式化文件跳 ...
- Program A-归并排序
Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...
- 这个setDefaultCloseOperation写不写的区别是什么?
2009-03-23 13:40提问者采纳 设置用户在此窗体上发起 "close" 时默认执行的操作.必须指定以下选项之一: DO_NOTHING_ON_CLOSE(在 W ...
- 黑客界大拿tombkeeper文章:怎么学好技术成为技术大拿(题目我自拟的)
这两天论坛上又有人开始抱怨世风日下,大家都现实了,都不开放了,不交流了.对这种“月经贴”,我基本上已经习惯了,不过因为吃了粉皮炖鸡,心情比较好,于是就说了两句. 三四年前,当时我对人性的看法还不像现在 ...
- 《JAVA学习笔记(1---13-4)》
[1]问题: 1.什么叫做面向过程? 2.什么叫做面向对象? 解答: 1: 所谓的面向过程就是我们是一个执行者,我们要开发一个项目,这个项目要求要实现很多功能,作为执行者的我们就需要 去一个一个的找这 ...
- 关于Json处理的两个实例
<script> var value1="{\"layer_datum\":{\"holdId\":\"dcdm\", ...
- objectARX获取当前图层所有文字样式
void GetAllTextStyle(std::vector<CString> &textStyle) { textStyle.clear(); AcDbTextStyleTa ...
- 转载--Ubuntu设置环境变量
Ubuntu设置环境变量并立即生效(以Ubuntu12.04为例) 标签: UbuntuLinux环境变量 2013-09-12 19:04 9961人阅读 评论(1) 收藏 举报 分类: Ubun ...
- FR #3题解
A. 傻逼题?...前缀和什么的随便搞搞就好了. #include<iostream> #include<cstdio> #include<cstring> #in ...
- Java custom annotations
Custom annotation definition is similar as Interface, just with @ in front. Annotation interface its ...