题目:

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. java多线程总结一:线程的两种创建方式及优劣比较

    1.通过实现Runnable接口线程创建 (1).定义一个类实现Runnable接口,重写接口中的run()方法.在run()方法中加入具体的任务代码或处理逻辑. (2).创建Runnable接口实现 ...

  2. mac配置svn服务器

    1创建svn库:mudydeMacBook-Air:~ mudy$   svnadmin create /Users/mudy/svn   2进行配置,配置我们的svn a.将svnsever.con ...

  3. oc 基础知识

    一.枚举 结构体 typedef enum{ sexMan, sexWoman }Sex;   tydedef struct{    int year;    int month;    int da ...

  4. RTTI(Runtime Type Information )

    RTTI 是“Runtime Type Information”的缩写,意思是:运行时类型信息.它提供了运行时确定对象类型的方法.本文将简略介绍 RTTI 的一些背景知识.描述 RTTI 的概念,并通 ...

  5. Registry uninstall values

    Original link: http://windowssucks.wordpress.com/win-registry-uninstall-values/ -------------------- ...

  6. 通过 ANE(Adobe Native Extension) 启动Andriod服务 推送消息(五)

    这一节,用个简单的例子来调用下之前生成的service.ane 首先建一个flex手机项目 然后在构建路径中把ane引进来 可以看到此ane支持Android平台. serviceMobile.mxm ...

  7. 51nod1046快速幂取余

    给出3个正整数A B C,求A^B Mod C.   例如,3 5 8,3^5 Mod 8 = 3. Input 3个正整数A B C,中间用空格分隔.(1 <= A,B,C <= 10^ ...

  8. pywinauto如何获取gridwindow控件的屏幕位置

    一:问题描述 问题一:通过查找pywinauto在线文档,其中没有讲解到gridwindow控件的方法,我不知道这个控件是不是标准控件,还是pywinauto根本就没适配这个控件.从网上查询了好多资料 ...

  9. writeToFile 读写文件问题

    关于 writeToFile 读写文件:当字典中键值对以 Model(例如:studentModel)为值时发现 Dictionary 调用 writeToFile 方法无法生成 plist 文件,经 ...

  10. 求解 s = (1*1)!+(2*2)! + (3*3)!+...+(n*n)! (C语言)

    提示:定义函数可以求阶乘,再定义函数求阶乘之和.1和0的阶乘是1,n(n > 1)的阶乘是n * (n-1) * (n - 2) * … * 1 //采用了函数嵌套调用和函数递归调用 //求解阶 ...