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 ...
随机推荐
- Spring AOP中pointcut expression表达式解析 及匹配多个条件
Spring中事务控制相关配置: <bean id="txManager" class="org.springframework.jdbc.datasource.D ...
- [转]AndroidManifest.xml文件详解
转自:http://www.cnblogs.com/greatverve/archive/2012/05/08/AndroidManifest-xml.html AndroidManifest.xml ...
- Python Web 应用:WSGI基础
在Django,Flask,Bottle和其他一切Python web 框架底层的是Web Server Gateway Interface,简称WSGI.WSGI对Python来说就像 Servle ...
- SharePoint 2013 Nintex Workflow 工作流帮助(十三)
博客地址 http://blog.csdn.net/foxdave 工作流动作 35. Delegate Workflow Task(User interaction分组) 该操作将委托未处理的工作流 ...
- C# Sandboxer
public static string IsolateCallV1(PageContentHandler pHandler) { string name = Guid.NewGuid().ToStr ...
- Ubuntu 14.10 下Ganglia监控Spark集群
由于Licene的限制,没有放到默认的build里面,所以在官方网站下载的二进制文件中并不包含Gangla模块,如果需要使用,需要自己编译.在使用Maven编译Spark的时候,我们可以加上-Pspa ...
- Android RecyclerView(瀑布流)水平/垂直方向分割线
Android RecyclerView(瀑布流)水平/垂直方向分割线 Android RecyclerView不像过去的ListView那样随意的设置水平方向的分割线,如果要实现Recycle ...
- 拆解cytom!c's 的keyFile保护
系统 : Windows xp 程序 : cytom!c's 程序下载地址 :http://pan.baidu.com/s/1nulAYBv 要求 : 伪造KeyFile 使用工具 :IDA & ...
- iOS教程
iOS绘图教程: http://www.cocoachina.com/industry/20140115/7703.html iOS基础教程:--包含脚本 swift http://bbs.zxope ...
- magento问题集
magento产品页面价格出现2遍 In app\design\frontend\default\default\template\catalog\product\view\type\Simple.p ...