博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
135. Candy
阅读量:7082 次
发布时间:2019-06-28

本文共 3730 字,大约阅读时间需要 12 分钟。

题目:

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?

链接: 

题解:

贪婪法,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 

转载地址:http://erlml.baihongyu.com/

你可能感兴趣的文章
SQL - 17.存储过程
查看>>
【梦话区】学习嵌入式需要哪些知识——转
查看>>
Qt添加信号槽
查看>>
vs2008调试 Release 工程
查看>>
dos下进入某一文件
查看>>
热烈庆祝博客园乐进博客正式开通
查看>>
hdu 1247 Hat’s Words(字典树)
查看>>
(转)查看修改oracle数据库字符集
查看>>
Simple way to export SQL Server data to Text Files
查看>>
Visual Studio 2012中使用自定义project properties
查看>>
引用 MySQL集群:主从数据库配置 实现查询负载
查看>>
选中复选框
查看>>
Asp.net web Api源码分析-HttpRequestMessage的创建
查看>>
Asp.net web Api源码分析-HttpActionDescriptor的创建
查看>>
免费图标字体:一套圣诞节相关的图标字体
查看>>
让datagridview默认选中一行,系统默认的是选中第一行的第一个单元格
查看>>
php创建多级目录的函数
查看>>
ASP.NET 服务器控件对应HTML标签
查看>>
C++中int 转换成 string类型
查看>>
汽车库通风防排烟设计规范
查看>>