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. Linux下搭建Oracle11g RAC(5)----配置ASM磁盘

    将共享磁盘格式化.然后用asmlib将其配置为ASM磁盘,用于将来存放OCR.Voting Disk和数据库用. 注意:只需在其中1个节点上格式化就可以,接下来我们选择在node1节点上格式化. 这里 ...

  2. ASP.NET中如何实现负载均衡

    ASP.NET站点中做负载均衡: 基于HTTP协议我们可能发现我们要解决两点问题: 第一,做到负载均衡,我们需要一个负载均衡器. 可以通过DNS轮询来做,在DNS服务器上配置为每次对我们做负载均衡的同 ...

  3. Prototype 原型模式

    用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象. 原型模式即在运行时动态的通过一个对象的实例来创建这个类的对象,可以理解成通过一个实例克隆出另一个实例. Prototype模式的一些优 ...

  4. HDU 4336 Card Collector(容斥)

    题意:要收集n种卡片,每种卡片能收集到的概率位pi,求收集完这n种卡片的期望.其中sigma{pi} <=1; 思路:容斥原理.就是一加一减,那么如何算期望呢.如果用二进制表示,0表示未收集到, ...

  5. samba环境搭建

    1.安装samba软件 sudo apt-get install samba cifs-utils samba-common 2.创建与windows共享目录 mkdir share chmod 77 ...

  6. Task类(任务)

    任务表示应完成的某个单元的工作.这个单元的工作可以在单独的线程中运行,也可以以同步方式启动一个任务,这需要等待主调用线程.使用任务不仅可以获得一个抽象层,还可以对底层线程进行很多控制. 1.启动任务 ...

  7. PHP之APC缓存详细介绍

    1.APC缓存简介 APC,全称是Alternative PHP Cache,官方翻译叫"可选PHP缓存".它为我们提供了缓存和优化PHP的中间代码的框架. APC的缓存分两部分: ...

  8. 【jQuery】用jQuery给文本框添加只读属性【readOnly】

    <input id="id" type="text" /> jQuery( $("#ID").attr({ readonly: ...

  9. 最近有机会接触到了angularJs

    记点笔记  概念多了 理顺还待时日: 总的来说: 1.ng-src src属性 2.ng-href href属性 3.ng-checked 选中状态 4.ng-selected 被选择状态 5.ng- ...

  10. 在MessageBox的Show方法中如何无限使用参数值?

    今天发现在show方法中不能使用花括号的方式使用多个可变参数,经过查询得出结果.在show方法中是不存在花括号的方式使用参数的.在Console.WriteLine中是存在的,如下: 那么在show方 ...