135. 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?
链接: http://leetcode.com/problems/candy/
题解:
贪婪法,O(n) space的比较简单,可以左右各来一遍,然后加起来。 不过要尝试更好的公司,还需要练习O(1) space的解法。二刷时再解决。
Time Complexity - O(n),Space Complexity - O(n)。
public class Solution {
public int candy(int[] ratings) {
if(ratings == null || ratings.length == 0)
return 0;
int len = ratings.length;
int[] candies = new int[len];
candies[0] = 1;
for(int i = 1; i < len; i++) {
if(ratings[i] > ratings[i - 1])
candies[i] = candies[i - 1] + 1;
else
candies[i] = 1;
}
int sum = candies[len - 1];
for(int i = len - 2; i >= 0; i--) {
if(ratings[i] > ratings[i + 1])
if(candies[i] <= candies[i + 1])
candies[i] = candies[i + 1] + 1;
sum += candies[i];
}
return sum;
}
}
二刷:
依然用的是O(n) space的解法,左右各来一遍。
Java:
public class Solution {
public int candy(int[] ratings) {
if (ratings == null || ratings.length == 0) return 0;
int len = ratings.length;
int[] candys = new int[len];
candys[0] = 1;
for (int i = 1; i < len; i++) {
if (ratings[i] > ratings[i - 1]) {
candys[i] = candys[i - 1] + 1;
} else {
candys[i] = 1;
}
}
for (int i = len - 2; i >= 0; i--) {
if (ratings[i] > ratings[i + 1] && candys[i] <= candys[i + 1]) {
candys[i] = candys[i + 1] + 1;
}
}
int sum = 0;
for (int count : candys) sum += count;
return sum;
}
}
O(1) space的方法来自Discuss里的@shpolsky
- 这里我们一次遍历数组, 主要使用一个变量countDown来记录遍历时遇到的递减序列
- 当ratings[i] < ratings[i - 1]时,我们遇到的就是递减序列,这时我们countDown增加一,
- 否则,ratings[i] >= ratings[i - 1],大于或者等于这两种情况里,我们都需要对之前遇到的递减情况进行处理
- 处理之前含有递减序列的情况
- 这里我们用prev这个变量记录了递减序列排头元素peak,有多少块糖
- 然后我们利用等差数列求和公式来计算这整个递减序列里我们需要补发多少块糖,countDown是长度n,也是最后一个元素an
- 之后还要判断,当countDown >= peak的时候,就是这个递减序列里,需要最多块糖的元素和peak的当前糖数比较,假如peak的糖数少,我们要给peak补充countDown - prev + 1块糖,或者理解为把peak所在位置的糖数从 prev 替换为 countDown + 1。
- 接下来我们处理一般情况,就是 ratings[i] = ratings[i - 1]时,prev为1,否则prev加1,我们再把prev添加到结果total中
- 处理之前含有递减序列的情况
- 最后也要判断一下,是否数组最后的一部分为递减序列,假如是,则按照之前的代码处理。
- 返回结果。
public class Solution {
public int candy(int[] ratings) {
if (ratings == null || ratings.length == 0) return 0;
int total = 1, prev = 1, countDown = 0;
for (int i = 1; i < ratings.length; i++) {
if (ratings[i] >= ratings[i - 1]) {
if (countDown > 0) {
total += countDown * (countDown + 1) / 2;
if (countDown >= prev) total += countDown - prev + 1;
countDown = 0;
prev = 1;
}
prev = (ratings[i] == ratings[i - 1]) ? 1 : prev + 1;
total += prev;
} else countDown++;
}
if (countDown > 0) {
total += countDown * (countDown + 1) / 2;
if (countDown >= prev) total += countDown - prev + 1;
}
return total;
}
}

Reference:
https://leetcode.com/discuss/76/does-anyone-have-a-better-idea
https://leetcode.com/discuss/43581/solutions-given-explanation-time-with-space-other-with-space
https://leetcode.com/discuss/16463/a-simple-solution
https://leetcode.com/discuss/8501/my-accepted-o-n-o-1-solution
https://leetcode.com/discuss/23835/one-pass-constant-space-java-solution
135. Candy的更多相关文章
- LeetCode 135 Candy(贪心算法)
135. Candy There are N children standing in a line. Each child is assigned a rating value. You are g ...
- leetcode 135. Candy ----- java
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- Leetcode#135 Candy
原题地址 遍历所有小孩的分数 1. 若小孩的分数递增,分给小孩的糖果依次+12. 若小孩的分数递减,分给小孩的糖果依次-13. 若小孩的分数相等,分给小孩的糖果设为1 当递减序列结束时,如果少分了糖果 ...
- (LeetCode 135) Candy N个孩子站成一排,给每个人设定一个权重
原文:http://www.cnblogs.com/AndyJee/p/4483043.html There are N children standing in a line. Each child ...
- [leet code 135]candy
1 题目 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(Array; Greedy)
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- Java for LeetCode 135 Candy
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- 135 Candy 分配糖果
There are N children standing in a line. Each child is assigned a rating value.You are giving candie ...
随机推荐
- [转]第一章 Windows Shell是什么 【来源:http://blog.csdn.net/wangqiulin123456/article/details/7987862】
一个操作系统外壳的不错的定义是它是一个系统提供的用户界面,它允许用户执行公共的任务,如访问文件系统,导出执行程序,改变系统设置等.MS-DOS有一个Command.COM扮演着这个角色.然而Windo ...
- partial与sorted
import functools sorted_ignore_case = functools.partial(sorted,cmp=lambda s1, s2: cmp(s1.upper(), s2 ...
- 安装oracle 12c遇到问题
安装前步骤:更改用户账户控制设置:从不通知 出现 "SEVERE: [FATAL] [INS-30014] 无法检查指定的位置是否位于 CFS 上" 解决办法:重新设置hosts ...
- 测试中的几个case
一.页面上对引起 大量数据提交的 按钮/链接 点击一次后, disable 需求: 对于重要的表单.数量庞大/响应慢的系统,在做提交时, 又有页面还在loading状态, 此时连续做两次点击, 经常 ...
- C#微信开发之旅--准备阶段
最近才开始学微信开发的相关内容,记录下,慢慢的养成习惯! 1.申请公众号: 公众号分为 订阅号 和 服务号.他们之前的区别可以点击这里查看 因为我们是测试的,所以可以直接申请测试帐号,就把所有的功能都 ...
- javascript异步执行函数导致的变量变化问题解决思路
for(var i=0;i<3;i++) { setTimeout(function(){ console.log(i) },0); }控制台输出:333 这是因为执行方法的时候for循环已经执 ...
- 13_ServletContext对象
[简介] ServletContext即Servlet上下文对象,该对象表示当前的web应用环境信息,一个Web应用只会创建一个ServletContext对象. Web容器启动的时候,它会为每个We ...
- 我的VIM.rc
我的VIM.rc """""""""""""""& ...
- [leetcode] 406. Queue Reconstruction by Height
https://leetcode.com/contest/6/problems/queue-reconstruction-by-height/ 分析:每个表示成(a,b)的形式,其实找第一个,就是b为 ...
- Headfirst设计模式的C++实现——适配器(Adapter)
duck.h #ifndef _DUCK_H_ #define _DUCK_H_ class DUCK { public: ; ; }; #endif mallard_duck.h #ifndef _ ...