题目:

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)。

  1. public class Solution {
  2. public int candy(int[] ratings) {
  3. if(ratings == null || ratings.length == 0)
  4. return 0;
  5. int len = ratings.length;
  6. int[] candies = new int[len];
  7. candies[0] = 1;
  8.  
  9. for(int i = 1; i < len; i++) {
  10. if(ratings[i] > ratings[i - 1])
  11. candies[i] = candies[i - 1] + 1;
  12. else
  13. candies[i] = 1;
  14. }
  15.  
  16. int sum = candies[len - 1];
  17.  
  18. for(int i = len - 2; i >= 0; i--) {
  19. if(ratings[i] > ratings[i + 1])
  20. if(candies[i] <= candies[i + 1])
  21. candies[i] = candies[i + 1] + 1;
  22. sum += candies[i];
  23. }
  24.  
  25. return sum;
  26. }
  27. }

二刷:

依然用的是O(n) space的解法,左右各来一遍。

Java:

  1. public class Solution {
  2. public int candy(int[] ratings) {
  3. if (ratings == null || ratings.length == 0) return 0;
  4. int len = ratings.length;
  5. int[] candys = new int[len];
  6. candys[0] = 1;
  7.  
  8. for (int i = 1; i < len; i++) {
  9. if (ratings[i] > ratings[i - 1]) {
  10. candys[i] = candys[i - 1] + 1;
  11. } else {
  12. candys[i] = 1;
  13. }
  14. }
  15.  
  16. for (int i = len - 2; i >= 0; i--) {
  17. if (ratings[i] > ratings[i + 1] && candys[i] <= candys[i + 1]) {
  18. candys[i] = candys[i + 1] + 1;
  19. }
  20. }
  21.  
  22. int sum = 0;
  23. for (int count : candys) sum += count;
  24. return sum;
  25. }
  26. }

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. 返回结果。
  1. public class Solution {
  2. public int candy(int[] ratings) {
  3. if (ratings == null || ratings.length == 0) return 0;
  4. int total = 1, prev = 1, countDown = 0;
  5.  
  6. for (int i = 1; i < ratings.length; i++) {
  7. if (ratings[i] >= ratings[i - 1]) {
  8. if (countDown > 0) {
  9. total += countDown * (countDown + 1) / 2;
  10. if (countDown >= prev) total += countDown - prev + 1;
  11. countDown = 0;
  12. prev = 1;
  13. }
  14. prev = (ratings[i] == ratings[i - 1]) ? 1 : prev + 1;
  15. total += prev;
  16. } else countDown++;
  17. }
  18.  
  19. if (countDown > 0) {
  20. total += countDown * (countDown + 1) / 2;
  21. if (countDown >= prev) total += countDown - prev + 1;
  22. }
  23.  
  24. return total;
  25. }
  26. }

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. VBA实现随意输入组合码,查询唯一标识码

    记录背景: 需要在excel中查询出组合码,对应的唯一标识码. 举例 组合码:4+5+6+9+1*2   标识码:A1 界面随意输入组合码:1*2+4+5+6+9  输出标识码:A1 VBA实现: P ...

  2. Docker Machine, Compose, and Swarm: How They Work Together

    The three tools are now neatly packaged into what’s called the Docker Toolbox. Docker Machine1/ crea ...

  3. 0708_Java如何设置输入流

    1.Java如何设置输入流:?(以解决看下面实例代码) 2.Java如何设置全局变量:(以解决public static即可) 3.Java为什么在做那种机试题目的时候都要设置成静态的:(以解决,因为 ...

  4. JAVA访问配置文件总结

    一.全局配置的简单 propertie 文件实现 package com.testgs.utils; import java.util.*; import java.io.*; public fina ...

  5. SVN服务器从Windows迁移到Linux

    gerui 2013.9.14 ge-rui@sohu.com 一.备份VisualSVN项目 1. 现在要使用Linux作为svn服务器,之前是在windows Server 2008上的,用的是V ...

  6. CKRule BRMS-决策表使用说明

    决策表的编辑有2个步骤,即规则包编辑.规则数据设置和发布. 1.   规则包编辑 双击CKBoot.exe打开CKRule规则引擎编辑界面.点新建,在左侧的工程窗口中,就会出现一个新的规则包.点击根结 ...

  7. 14_CXF发布REST服务

    [rest服务] REST服务是一种软件架构模式,只是一种风格.REST服务采用HTTP做传输协议. REST对于HTTP的利用分为以下两种: 一.资源定位 REST要求对方资源定位更加准确,如下: ...

  8. undrop for innodb c_parser 源码分析

    一,主函数功能: 1,分析命令行参数,保存在全局变量中; 2,打开文件,加载表定义sql,调用分析函数开始处理; 3,打印导入数据的sql语句; 二,文件处理函数,void process_ibfil ...

  9. sgu 108 Self-numbers II

    这道题难在 hash 上, 求出答案很简单, 关键是我们如何标记, 由于 某个数变换后最多比原数多63 所以我们只需开一个63的bool数组就可以了! 同时注意一下, 可能会有相同的询问. 我为了防止 ...

  10. mysql主配置文件my.cnf详细说明

    MySQL配置文件my.cnf 例子最详细翻译,可以保存做笔记用[转载]#BEGIN CONFIG INFO#DESCR: 4GB RAM, 只使用InnoDB, ACID, 少量的连接, 队列负载大 ...