1 题目

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?

接口

int candy(int[] ratings)
几个小孩站一排,每个小孩有个等级值,现在给小孩发糖,发的时候要遵守2个规则:(1)每个小孩至少一颗糖(2)两个相邻的小孩中,等级大的小孩一定比等级小的小孩糖多,求发糖的数目的最小值。

2 思路

基本思路:先进行2次扫描,一次从左往右,一次从右往左。最后一次扫描累加出结果。
第一次扫描:维护对于每一个小孩左边所需要最少的糖果数量,存入数组对应元素中。
第二次扫描:维护右边所需的最少糖果数。
第三次扫描:将左边和右边大的糖果数量累加到result,从而累加得出结果。
example: ratings = [3,4,5,1,2,3]
 
ratings 3 4 5 1 2 3
lefts 1 2 3 1 2 3
rights 1 1 2 1 1 1
两者最大值 1 2 3 1 2 3
result = sum(1,2,3,1,2,3) = 12

复杂度

方法只需要3次扫描,所以时间复杂度是O(3*n)=O(n)。空间上需要2个长度为n的数组,复杂度是O(n)。

3 代码

     public int candy(int[] ratings) {
final int len = ratings.length;
int[] lefts = new int[len];
int[] rights = new int[len]; // scan from left to right
lefts[0] = 1;
for (int i = 1; i < len; i++) {
if (ratings[i] > ratings[i - 1]) {
lefts[i] = lefts[i - 1] + 1;
} else {
lefts[i] = 1;
}
} // scan from right to left
rights[len - 1] = 1;
for (int i = len - 2; i >= 0; i--) {
if (ratings[i] > ratings[i + 1]) {
rights[i] = rights[i + 1] + 1;
} else {
rights[i] = 1;
}
} // calculate min nums of candy
int result = 0;
for (int i = 0; i < len; i++) {
result += Math.max(lefts[i], rights[i]);
}
return result;
}

4 总结

  • 小朋友分糖,影响因素都是由左右两边的最值来决定的。
  • 这种两边扫描的方法是一种比较常用的技巧,LeetCode中Trapping Rain Water和这道题都用到了,可以把这种方法作为自己思路的一部分,通常是要求的变量跟左右元素有关系的题目会用到。 From Code Ganker
  • code可以将第三次扫描省掉,把第三次扫描的工作放到第二次里面做,现在不推荐做,因为显得结题思路不清晰。

5 扩展

Trapping Rain Water

6 参考

lc面试准备:Candy的更多相关文章

  1. lc面试准备:Remove Duplicates from Sorted List

    1 题目 Given a sorted linked list, delete all duplicates such that each element appear only once. For ...

  2. lc面试准备:Implement Stack using Queues

    1 题目 Implement the following operations of a stack using queues. push(x) -- Push element x onto stac ...

  3. lc面试准备:Implement Queue using Stacks

    1 题目 Implement the following operations of a queue using stacks. push(x) -- Push element x to the ba ...

  4. lc面试准备:Invert Binary Tree

    1 题目 Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 接口: public TreeNod ...

  5. lc面试准备:Power of Two

    1 题目 Given an integer, write a function to determine if it is a power of two. 接口 boolean isPowerOfTw ...

  6. lc面试准备:Repeated DNA Sequences

    1 题目 All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: &quo ...

  7. lc面试准备:Number of 1 Bits

    1 题目 Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also ...

  8. lc面试准备:Reverse Bits

    1 题目 Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represente ...

  9. lc面试准备:Regular Expression Matching

    1 题目 Implement regular expression matching with support for '.' and '*'. '.' Matches any single char ...

随机推荐

  1. Oracle 设置日志模式

    在NOARCHIVELOG模式下启动和运行一个数据库. 确定闪回恢复区的位置和归档日志目标目录的位置. 步骤一 为归档的重做日志配置FRA和单独的归档日志目标. 首先,设置FRA参数DB_RECOVE ...

  2. dbartisan下载地址

    http://downloads.embarcadero.com/free/dbartisan

  3. Property工具类,Properties文件工具类,PropertiesUtils工具类

    Property工具类,Properties文件工具类,PropertiesUtils工具类 >>>>>>>>>>>>>& ...

  4. plsql中文乱码问题(显示问号)

    问题:打开plsql,执行sql语句,中文显示乱码: 解决方案: 输入sql语句select userenv('language') from dual查看数据库字符集 输入sql语句select * ...

  5. 还原data block dumps实际值

    前天看了一个案例因为丢了表上的数据,从索引block中找回了值 转储了oracle block的值,如何得到它真正表中的值,也算 是dump(val,16)的逆运算 sys@ORCL>conn ...

  6. OC相关-02:oc和c的基本差异

    前言: 面向过程和面向对象. 简单的说,面向过程就是分析出解决问题所需要的步骤,然后用函数把这些步骤一步一步实现,使用的时候一个一个依次调用. 面向对象是把构成问题事务分解成各个对象,建立对象的目的不 ...

  7. wamp使用方法【总】

    Apache与php配置:我们把php-5.2.9-Win32.zip解压到C盘根目录下,把文件夹名字改成PHP,这样方便一下. 1. 找到PHP目录下的“php.ini-dist”或者“php.in ...

  8. CSS 列表

    CSS列表属性作用如下: 设置不同的列表项标记为有序列表 设置不同的列表项标记为无序列表 设置列表项标记为图像 列表 在HTML中,有两种类型的列表: 无序列表 - 列表项标记用特殊图形(如小黑点.小 ...

  9. HDU2035 人见人爱A^B(快速幂)

    描述: 求A^B的最后三位数表示的整数.说明:A^B的含义是“A的B次方”. 输入: 输入数据包含多个测试实例,每个实例占一行,由两个正整数A和B组成(1<=A,B<=10000),如果A ...

  10. Qt中使用信号和槽的一点心得

    信号(Signal)与槽(Slot)-Qt中的典型机制 这一篇文章中都说得很详细了,这里不再重复,只说一点在实际使用中可能会遇到的问题. 1.一个信号不要同时连接几个槽函数,不然执行的顺序是随机的,最 ...