Candy 解答
Question
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?
Solution
Key to the problem is to consider the whole array as combination of many ascending sub-arrays and descending sub-arrays. So when we solve candy distribution problems among these sub-arrays, the whole problem is solved.
There are some points to note:
1. To find descending sub-array like 5, 4, 3, 2, 1, we can traverse from right to left. In this way, the sub-array is ascending.
2. For each peek, like 5 in 1, 2, 3, 4, 5, 4, 1, we need compare its previous candy number with current candy number.
Example
public class Solution {
public int candy(int[] ratings) {
if (ratings == null || ratings.length < 1)
return 0;
int length = ratings.length;
if (length == 1)
return 1;
int[] candyNum = new int[length];
candyNum[0] = 1;
int result = 0;
int index = 1;
// First, process ascending sub-array
while (index < length) {
if (ratings[index] > ratings[index - 1])
candyNum[index] = candyNum[index - 1] + 1;
else
candyNum[index] = 1;
index++;
} // Then, process descending sub-array
index = length - 2;
result = candyNum[length - 1];
while (index >= 0) {
if (ratings[index] > ratings[index + 1])
// Here, we need compare
candyNum[index] = Math.max(candyNum[index + 1] + 1, candyNum[index]);
result += candyNum[index];
index--;
} return result;
}
}
Candy 解答的更多相关文章
- LeetCode算法题目解答汇总(转自四火的唠叨)
LeetCode算法题目解答汇总 本文转自<四火的唠叨> 只要不是特别忙或者特别不方便,最近一直保持着每天做几道算法题的规律,到后来随着难度的增加,每天做的题目越来越少.我的初衷就是练习, ...
- [LeetCode] Candy 分糖果问题
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- 精选30道Java笔试题解答
转自:http://www.cnblogs.com/lanxuezaipiao/p/3371224.html 都 是一些非常非常基础的题,是我最近参加各大IT公司笔试后靠记忆记下来的,经过整理献给与我 ...
- 精通Web Analytics 2.0 (8) 第六章:使用定性数据解答”为什么“的谜团
精通Web Analytics 2.0 : 用户中心科学与在线统计艺术 第六章:使用定性数据解答"为什么"的谜团 当我走进一家超市,我不希望员工会认出我或重新为我布置商店. 然而, ...
- 【字符编码】Java字符编码详细解答及问题探讨
一.前言 继上一篇写完字节编码内容后,现在分析在Java中各字符编码的问题,并且由这个问题,也引出了一个更有意思的问题,笔者也还没有找到这个问题的答案.也希望各位园友指点指点. 二.Java字符编码 ...
- spring-stutrs求解答
这里贴上applicationContext里的代码: <?xml version="1.0" encoding="UTF-8"?> <bea ...
- JavaScript Bind()趣味解答 包懂~~
首先声明一下,这个解答是从Segmentfault看到的,挺有意思就记录下来.我放到最下面: bind() https://developer.mozilla.org/zh-CN/docs/Web/J ...
- Leetcode Candy
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- LeetCode 135 Candy(贪心算法)
135. Candy There are N children standing in a line. Each child is assigned a rating value. You are g ...
随机推荐
- 技术小白:Hadoop 到底是啥?
原文地址:http://os.51cto.com/art/201305/396145.htm 大数据是个铺天盖地的词,而谈论大数据又不可避免地要提到Hadoop,遗憾的是今天大多数大数据鼓吹者,甚至专 ...
- poj2262
Goldb ...
- POJ 3046 Ant Counting DP
大致题意:给你a个数字,这些数字范围是1到t,每种数字最多100个,求问你这些a个数字进行组合(不包含重复),长度为s到b的集合一共有多少个. 思路:d[i][j]——前i种数字组成长度为j的集合有多 ...
- Thinkphp多表联查mysql写法
$model=M("user","","mysql://root:222222@localhost:3306/jiaoyou"); //换数 ...
- Direct3D 对X模型载入
今天我们来学习Direct3D对模型的导入使用,Direct3D支持.X模型文件导入使用,.X文件是微软定义的3D模型文件格式,其中包含网格,动画,纹理等等一些信息. 目前3DS Max 和 Maya ...
- ThinkPHP整合百度Ueditor图文教程
ThinkPHP整合百度Ueditor图文教程 ThinkPHP整合百度Ueditor,基于黄永成老师的视频说明的申明:最好大家都能写绝对路径的都写好绝对路径比如:window.UEDITOR_HOM ...
- 函数nvl,nvl2,nullif,coalesce
NVL: Converts a null value to an actual valueNVL2:If expr1 is not null, NVL2 returns expr2. If expr1 ...
- css解决文字垂直居中
参考链接: http://www.cnblogs.com/lufy/archive/2012/09/12/2681972.html http://zhidao.baidu.com/question ...
- Chrome开发者工具详解 (5):Application、Security、Audits面板
Application面板简介 该面板主要是记录网站加载的所有资源信息,包括存储数据(Local Storage.Session Storage.IndexedDB.Web SQL.Cookies). ...
- 第二章 Android Studio使用第三方模拟器
1.为什么要使用第三方模拟器 Android Studio自带模拟器,相对Eclipse来说项目启动速度的确快了很多倍,提高了开发效率.但和第三方模拟器进行对比的话,还是第三方的模拟器运行速度更快些. ...