lc面试准备:Candy
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 思路
| 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 |
复杂度
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 扩展
6 参考
lc面试准备:Candy的更多相关文章
- lc面试准备:Remove Duplicates from Sorted List
1 题目 Given a sorted linked list, delete all duplicates such that each element appear only once. For ...
- lc面试准备:Implement Stack using Queues
1 题目 Implement the following operations of a stack using queues. push(x) -- Push element x onto stac ...
- lc面试准备:Implement Queue using Stacks
1 题目 Implement the following operations of a queue using stacks. push(x) -- Push element x to the ba ...
- 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 ...
- lc面试准备:Power of Two
1 题目 Given an integer, write a function to determine if it is a power of two. 接口 boolean isPowerOfTw ...
- lc面试准备:Repeated DNA Sequences
1 题目 All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: &quo ...
- lc面试准备:Number of 1 Bits
1 题目 Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also ...
- lc面试准备:Reverse Bits
1 题目 Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represente ...
- lc面试准备:Regular Expression Matching
1 题目 Implement regular expression matching with support for '.' and '*'. '.' Matches any single char ...
随机推荐
- android开发布局优化之ViewStub
使用ViewStub可以延迟加载一个布局文件,提高显示速率.刚开始接触到,记录下来. 关于viewstub的使用,我们可以在不同的布局中使用,比如可以根据设备的大小动态决定显示哪个界面. viewst ...
- Linux下Tomcat安装、配置
/etc/profile./etc/profile.d和.bash_profile区别 /etc/profile和/etc/profile.d区别 .bash_profile 是存放用户的全局变量 / ...
- CS=0xFFFF IP=0x0000与CS=F000 IP=FFF0
计算机自动上电后,有些书上说CS=0xFFFF IP=0x0000,例如linux内核设计的艺术(第三版).也有一些书说CS=F000 IP=FFF0,例如赵炯的linux内核完全注释. 其实并不是说 ...
- 【jQuery】用jQuery给文本框添加只读属性【readOnly】
<input id="id" type="text" /> jQuery( $("#ID").attr({ readonly: ...
- (转)Hibernate 的应用(Hibernate 的结构)?
//首先获得 SessionFactory 的对象 SessionFactory sessionFactory = new Configuration().configure(). buildSess ...
- Javascript 汉字转拼音
调用方式: var pinyin = convert("欢迎光临"); alert(pinyin); 新建JS文件:PYConvert.js,内容如下: var PinYin = ...
- 网络解析之XML及JSON
首先要加入类库GDataXMLNode和JSON 解析本地文件Students.txt <students> <student> <name>汤姆 </nam ...
- ACM HDU 1021 Fibonacci Again
#include<iostream> using namespace std; int main() { int n; while(cin>>n) { if((n+1)%4== ...
- ubuntu 14.04安装amd omega 驱动
驱动共分4部分: fglrx_14.501-0ubuntu1_amd64_UB_14.01.deb 52.0MB fglrx-core_14.501-0ubuntu1_amd64_UB_14 ...
- Linux 消息队列编程
消息队列.信号量以及共享内存被称作 XSI IPC,它们均来自system V的IPC功能,因此具有许多共性. 键和标识符: 内核中的每一种IPC结构(比如信号量.消息队列.共享内存)都用一个非负整数 ...