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 ...
随机推荐
- Ext信息提示对话框
Ext.window.MessageBox是一个工具类,他继承自Ext.window.Windoe对象,用来生成各种风格的信息提示对话框,其实例对象可以通过Ext.MessageBox或Ext.Msg ...
- 批处理,修改环境变量path的方法(加环境变量)
方法一:批处理中,修改环境变量,一次性有效(也就是在当前的脚本中有效) CMD中运行 set path==%path%;d:/mypath 用 set path可以查看,当前的环境变量 方法二 :批处 ...
- Eclipse将项目部署tomcat的webapps目录
Eclipse将项目部署tomcat的webapps目录 >>>>>>>>>>>>>>>>>& ...
- 免费的手机号码归属地查询API接口文档
聚合数据手机号码归属四查询API接口,根据手机号码或手机号码的前7位,查询手机号码归属地信息,包括省份 .城市.区号.邮编.运营商和卡类型. 通过链接https://www.juhe.cn/docs/ ...
- wordpress密码生成与登录密码验证
一.研究wordpress时wordpess的密码密码生成与登录密码验证方式很重要 WordPress密码已成为整合的首要目标,如何征服整合,就得了解WordPress密码算法. WordPress系 ...
- Unity5.0 手动激活
提供Unity5.0.1.f1(32-bit)下载http://pan.baidu.com/s/1bg5sDK 密码 ns75 有时候会发现,用激活工具是激活不了的,这个时候就要手动激活,其实个人觉得 ...
- Activity是如何挂载Pargment的Day35
Activity是如何挂载Pargment的Day35 mebile5.0 1.Fragment优化早上任务 思路 一个主Activity 短信群发器 中秋给朋友发短信,都能够加上他们的姓名,这样他们 ...
- 如何获得Windows 8中已记住的WIFI的明文密码
网上很流行的一种查看WIFI密码明文的方法,如下: 今天遇到了一种状况,就是如果不连WIFI的情况我能抓到这个密码吗?(实在不想开口问同事密码多少,只能苦逼的自己想办法了o(︶︿︶)o ) 答案当然是 ...
- SQL打印全年日历
数据库环境:SQL SERVER 2008R2 我之前有写过打印本月日历的SQL,里头有详细的说明.具体请参考前面的博文——生成本月日历. 全年日历只是在本月日历的基础上加了月信息,并按月份分组求得. ...
- POJ_1088 滑雪(记忆型DP+DFS)
Description Michael喜欢滑雪,这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...