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 ...
随机推荐
- 关于Microsoft app下同义词的整理
Windows os 以下词表达的是同一个概念 windows store app windows metro app windows modern app windows runtime app w ...
- BackgroundWorker用法
BackgroundWorker主要用来提供后台运算服务(防止用户前台无响应等待),并提供服务进度的类: 代码如下: BackgroundWorker bgw = new BackgroundWork ...
- qt画刷和画笔
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #画刷和画笔:QBrush 定义了 QPainter 的填充模式,具 ...
- 创建UILabel
UILabelCreate.h #import <UIKit/UIKit.h> @interface UILabelCreate : UILabel /** * 创建UILabel 初始化 ...
- Go语言程序的状态监控
Go是很实在的编程语言,从一开始就提供了很详细的运行状态信息.产品上线后的调优和排查疑难杂症都得靠这些状态信息.这边总结一些我们项目里用到的状态监控手段. pprof Go自带了一个pprof工具,这 ...
- BOOST 线程完全攻略 - 结束语
modulethread扩展多线程破解通讯 全文介绍了3个boost::thread的扩展类,希望能给大家书写多线程代码带来便捷. thread -> controlled_module_ex ...
- Audio笔记之MediaPlayerService:setDataSource
//下面是一个典型的播放序列: MediaPlayer player=new MediaPlayer() player->setDataSource(url,header); player-&g ...
- jquery插件autocomplete
项目中有时会用到自动补全查询,就像Google搜索框.淘宝商品搜索功能,输入汉字或字母,则以该汉字或字母开头的相关条目会显示出来供用户选择, autocomplete插件就是完成这样的功能. < ...
- 如何利用自己的电脑做服务器发布tomcat的WEB项目供外网访问
1.首先你要确定你有一个外网ip地址.如果你分配到的是一个局域网IP地址需要经过一系列的转换为外网ip地址,然后继续下面操作. 2.拿到外网IP地址,进行tomcat的server.xml文件的配置. ...
- MySQL 基础 之 语句执行顺序
FORM: 对FROM的左边的表和右边的表计算笛卡尔积.产生虚表VT1 ON: 对虚表VT1进行ON筛选,只有那些符合<join-condition>的行才会被记录在虚表VT2中. JOI ...