题目:

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

  1. 这里我们一次遍历数组, 主要使用一个变量countDown来记录遍历时遇到的递减序列
  2. 当ratings[i] < ratings[i - 1]时,我们遇到的就是递减序列,这时我们countDown增加一,
  3. 否则,ratings[i] >= ratings[i - 1],大于或者等于这两种情况里,我们都需要对之前遇到的递减情况进行处理
    1. 处理之前含有递减序列的情况
      1. 这里我们用prev这个变量记录了递减序列排头元素peak,有多少块糖
      2. 然后我们利用等差数列求和公式来计算这整个递减序列里我们需要补发多少块糖,countDown是长度n,也是最后一个元素an
      3. 之后还要判断,当countDown >= peak的时候,就是这个递减序列里,需要最多块糖的元素和peak的当前糖数比较,假如peak的糖数少,我们要给peak补充countDown - prev + 1块糖,或者理解为把peak所在位置的糖数从 prev 替换为 countDown + 1。
    2. 接下来我们处理一般情况,就是 ratings[i] = ratings[i - 1]时,prev为1,否则prev加1,我们再把prev添加到结果total中
  4. 最后也要判断一下,是否数组最后的一部分为递减序列,假如是,则按照之前的代码处理。
  5. 返回结果。
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的更多相关文章

  1. LeetCode 135 Candy(贪心算法)

    135. Candy There are N children standing in a line. Each child is assigned a rating value. You are g ...

  2. leetcode 135. Candy ----- java

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

  3. Leetcode#135 Candy

    原题地址 遍历所有小孩的分数 1. 若小孩的分数递增,分给小孩的糖果依次+12. 若小孩的分数递减,分给小孩的糖果依次-13. 若小孩的分数相等,分给小孩的糖果设为1 当递减序列结束时,如果少分了糖果 ...

  4. (LeetCode 135) Candy N个孩子站成一排,给每个人设定一个权重

    原文:http://www.cnblogs.com/AndyJee/p/4483043.html There are N children standing in a line. Each child ...

  5. [leet code 135]candy

    1 题目 There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  6. 【LeetCode】135. Candy

    Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  7. 135. Candy(Array; Greedy)

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

  8. Java for LeetCode 135 Candy

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

  9. 135 Candy 分配糖果

    There are N children standing in a line. Each child is assigned a rating value.You are giving candie ...

随机推荐

  1. SQL SERVER 与ACCESS、EXCEL的数据导入导出转换

    * 说明:复制表(只复制结构,源表名:a 新表名:b)      select * into b from a where 1<>1 * 说明:拷贝表(拷贝数据,源表名:a 目标表名:b) ...

  2. ExtJs store加载

    当store加载数据对象中,如果带有success:false,store会认为加载数据失败,不予将得到的数据显示在界面上,所有记得在返回数据对象的同时返回success:true;

  3. 利用sys.dm_db_index_physical_stats查看索引碎片等数据(转)

    我们都知道,提高sql server的数据查询速度,最有效的方法,就是为表创建索引,而索引在对数据进行新增,删除,修改的时候,会产生索引碎片,索引碎片多了,就需要重新组织或重新生成索引,以达到索引的最 ...

  4. Oracle PL/SQL 事物处理 银行转账

    Oracle数据库中的事务处理:添加,修改,删除时需要使用事务处理(显示事务). 1.事务的分类显示事务(添加,修改,删除)和隐式事务(除了添加,修改,删除). 2.事务的执行方式:自动提交(jdbc ...

  5. 学习C++ Primer 的个人理解(三)

    第三章,主要内容是字符串和数组.感觉作者的意图是希望读者可以早一点可以写出简单的小程序,并且可以早点接触迭代器这种思想. 在我看来,这种内容的难度并不大. 对于编程来说,最重要的应该是思想,类似vec ...

  6. grant授权“失败”的原因

    在创建用户的时候我们通常采用grant命令完成,并同时赋予相应的权限,例如我们创建一个名为test的用户,g并赋予其对数据库foo下所有表格select,delete,drop,create权限: g ...

  7. python 自动化之路 day 05

    内容目录: 列表生成式.迭代器&生成器 装饰器 软件目录结构规范 模块初始 常用模块 1.列表生成式,迭代器&生成器 列表生成式 需求:列表[0, 1, 2, 3, 4, 5, 6, ...

  8. Make和Makefile

    无论是在Linux还是在Unix环境中,make都是一个非常重要的编译命令.不管是自己进行项目开发还是安装应用软件,我们都经常要用到make或make install.利用make工具,我们可以将大型 ...

  9. Python 基础篇:编码、变量、模块

    1. 编码 python解释器在加载 .py 文件中的代码时,会对内容进行编码(默认ASCII). 2. 变量 变量定义的规则: 变量名只能是 字母.数字或下划线的任意组合 变量名的第一个字符不能是数 ...

  10. 【JSTL EL】 jsp 页面学习

    JSTL(JSP Standard Tag Library,JSP标准标签库)是一个不断完善的开放源代码的JSP标签库,是由apache的jakarta小组来维护的.JSTL只能运行在支持JSP1.2 ...