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?

本身题目我感觉不是太难。

楼主是这样想的。

使用一个candy数组[0...ratings.length - 1]

初始化第一个孩子得到一个糖,也就是candy[0] = 1;

从1开始扫描这个ratings数组,

如果遇到上升序列,candy[i] = candy[i - 1] + 1,同时拿一个lastHigh变量记录当前上升序列的index。换句话说,lastHigh存放的就是上一次波峰的峰值的index

如果遇到相等的,candy[i] = 1。这里如果是face2face面试,你应该向面试官询问,如果ratings相等的孩子是否需要拿一样的糖。本题目没有这个要求,所以如果遇到相等的,我们立刻给一个糖,然后更新lastHigh。我第一次想的时候,坚信“lastHigh存放的就是上一次波峰的峰值的index”,所以我认为,“此步骤中,仅当ratings[lastHigh] == ratings[i], 才更新lastHigh为i”。这样,遇到相等的波峰,lastHigh始终指向最后一个高点(悬崖边上:>)。

如果遇到下降序列,这里有点复杂:

1)首先candy[i] = 1,因为要最小数目的糖嘛;

2)然后对从lastHigh + 1 到 i - 1 的candy,我们都要+1,那么,换句话说,total的糖的数目增加了i - lastHigh。注意到这是个优化,我们不需要真实地去遍历candy[lastHigh...i],依次去加1。

3)到了lastHigh,注意由于下降序列可能长度很长,造成candy[lastHigh + 1] == candy[lastHigh],那么,这时候我们也需要增加candy[lastHigh]。否则这一步不用做(candy[lastHigh] > candy[lastHigh + 1)

代码如下:

public int candy(int[] ratings) {
if (ratings.length == 0) {
return 0;
}
int[] candy = new int[ratings.length];
candy[0] = 1;
int lastHigh = 0;
int total = 1;
for(int i = 1; i < ratings.length; i++) {
if (ratings[i] > ratings[i - 1]) {
candy[i] = candy[i - 1] + 1;
lastHigh = i;
total += candy[i];
} else if (ratings[i] == ratings[i - 1]) {
candy[i] = 1 ;
if (ratings[lastHigh] == ratings[i]) {//走到波峰的悬崖的边上
lastHigh = i;
}
total += candy[i];
} else {
candy[i] = 1;
total += i - lastHigh; if (i != lastHigh + 1) {
candy[lastHigh + 1]++;
} if (candy[lastHigh] == candy[lastHigh + 1]) {
candy[lastHigh]++;
total += 1;
}
}
}
//assertValid(ratings, candy);
return total;
}

然后,就悲催地发现,超大集合怎么都WA。

然后卡了很久(电影字幕:”十年后。。。“)

楼主把最后一个case的数组下载了下来,一看有9999个元素。只能写了一个文件去读。

因为自信算法上没有什么问题,肯定是什么corner case没考虑到,于是就在算法介绍后面写了个assert 函数去确认candy数组的合法性。主要就是考虑对每个元素,如果ratings大于左右两个邻居,那么candy数组也是如此。不过,我怀疑在判断相等ratings会不会有什么幺蛾子,就同时assert了:对于相等连续的rating元素,他们不能得到candy都一样(否则就不是最小candy数目了啊)。于是发现了这个序列(当前值是i,在3250):

i

ratings:   8116   8116

candy:   2        1         1        0

看出问题了么?lastHigh当时是在9734.

如果继续更新,按照算法,

candy: 3        2          2        1

可是,最小糖,对这个序列来说,应该是:

candy:   2        1         2          1

想了一下,原来lastHigh,不应该是记录波峰的,它的意义应该是:对于当前的i来说,记录能够随意增长而不受到前方其他限制的元素的index。

例如,在上面这个序列中,相等连续序列的最后一个(8116)应该是lastHigh,因为它能够无限制的增长,一满足后面更小的ratings抬高。

所以,上面代码唯一的改动,就是在遇到相等序列的时候,及时更新lastHigh变量。

public int candy(int[] ratings) {
if (ratings.length == 0) {
return 0;
}
int[] candy = new int[ratings.length];
candy[0] = 1;
int lastHigh = 0;
int total = 1;
for(int i = 1; i < ratings.length; i++) {
if (ratings[i] > ratings[i - 1]) {
candy[i] = candy[i - 1] + 1;
lastHigh = i;
total += candy[i];
} else if (ratings[i] == ratings[i - 1]) {
candy[i] = 1 ;
lastHigh = i;
total += candy[i];
} else {
candy[i] = 1;
total += i - lastHigh; if (i != lastHigh + 1) {
candy[lastHigh + 1]++;
} if (candy[lastHigh] == candy[lastHigh + 1]) {
candy[lastHigh]++;
total += 1;
}
}
}
//assertValid(ratings, candy);
return total;
}

LeetCode 笔记25 Candy (艰难的调试)的更多相关文章

  1. Leetcode 笔记 36 - Sudoku Solver

    题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...

  2. 程序的载入和运行(五)——《x86汇编语言:从实模式到保护模式》读书笔记25

    程序的载入和运行(五)--<x86汇编语言:从实模式到保护模式>读书笔记25 前面几篇博文最终把代码分析完了.这篇就来说说代码的编译.运行和调试. 1.代码的编译及写入镜像文件 之前我们都 ...

  3. leetcode笔记——35.搜索插入位置 - CrowFea

    0.问题描述 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元素. 示例 1: 12 输入: [1,3 ...

  4. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

随机推荐

  1. vs(vistual studio)项目文件名字重复问题

    今天遇到一情况,比较神奇,vs项目,我更新SVN的时候,发现竟然出现文件名字重复的现象. [caption id="" align="alignnone" wi ...

  2. javascript 调试技巧

    不用alert,用console.log() <!DOCTYPE html> <html> <head> <script type="text/ja ...

  3. Java 8 LongAdders:管理并发计数器的正确方式

    转自:http://www.importnew.com/11345.html 我只是喜欢新鲜的事物,而Java 8 有很多新东西.这次我想讨论其中我最喜欢的之一:并发加法器.这是一个新的类集合,他们用 ...

  4. 你真的说的清楚ArrayList和LinkedList的区别吗

    参见java面试的程序员,十有八九会遇到ArrayList和LinkedList的区别?相信很多看到这个问题的人,都能回答个一二.但是,真正搞清楚的话,还得花费一番功夫. 下面我从4个方面来谈谈这个问 ...

  5. nginx 负载均衡示例

    一.nginx nginx是一个轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,以开源形式发布.nginx的性能稳定,功能丰富,配置简单,且占用系统资源低.可支持多个 ...

  6. Linux syslog介绍

    一.简介 syslog是Linux系统默认的日志守护进程.默认的主配置文件和辅助配置文件分别是/etc/syslog.conf和/etc/sysconfig/syslog文件.通常,syslog 接受 ...

  7. 有限状态机HDL模板

    逻辑设计, 顾名思义, 只要理清了逻辑和时序, 剩下的设计只是做填空题而已. 下面给出了有限状态机的标准设计,分别为 VHDL 和 Verilog 代码 1  有限状态机 2  VHDL模板之一 li ...

  8. UESTC 917 方老师的分身IV --求欧拉路径

    判断欧拉路径是否存在及求出字典序最小的欧拉路径问题(如果存在). 将字符串的第一个字母和最后一个字母间连边,将字母看成点,最多可能有26个点(a-z),如果有欧拉路径,还要判断是否有欧拉回路,如果有, ...

  9. HDU 5057 Argestes and Sequence --树状数组(卡内存)

    题意:给n个数字,每次两种操作: 1.修改第x个数字为y. 2.查询[L,R]区间内第D位为P的数有多少个. 解法:这题当时被卡内存了,后来看了下别人代码发现可以用unsigned short神奇卡过 ...

  10. 三维网格去噪算法(bilateral filter)

    受图像双边滤波算法的启发,[Fleishman et al. 2003]和[Jones et al. 2003]分别提出了利用双边滤波算法对噪声网格进行光顺去噪的算法,两篇文章都被收录于当年的SIGG ...