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 解答的更多相关文章

  1. LeetCode算法题目解答汇总(转自四火的唠叨)

    LeetCode算法题目解答汇总 本文转自<四火的唠叨> 只要不是特别忙或者特别不方便,最近一直保持着每天做几道算法题的规律,到后来随着难度的增加,每天做的题目越来越少.我的初衷就是练习, ...

  2. [LeetCode] Candy 分糖果问题

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

  3. 精选30道Java笔试题解答

    转自:http://www.cnblogs.com/lanxuezaipiao/p/3371224.html 都 是一些非常非常基础的题,是我最近参加各大IT公司笔试后靠记忆记下来的,经过整理献给与我 ...

  4. 精通Web Analytics 2.0 (8) 第六章:使用定性数据解答”为什么“的谜团

    精通Web Analytics 2.0 : 用户中心科学与在线统计艺术 第六章:使用定性数据解答"为什么"的谜团 当我走进一家超市,我不希望员工会认出我或重新为我布置商店. 然而, ...

  5. 【字符编码】Java字符编码详细解答及问题探讨

    一.前言 继上一篇写完字节编码内容后,现在分析在Java中各字符编码的问题,并且由这个问题,也引出了一个更有意思的问题,笔者也还没有找到这个问题的答案.也希望各位园友指点指点. 二.Java字符编码 ...

  6. spring-stutrs求解答

    这里贴上applicationContext里的代码: <?xml version="1.0" encoding="UTF-8"?> <bea ...

  7. JavaScript Bind()趣味解答 包懂~~

    首先声明一下,这个解答是从Segmentfault看到的,挺有意思就记录下来.我放到最下面: bind() https://developer.mozilla.org/zh-CN/docs/Web/J ...

  8. Leetcode Candy

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

  9. LeetCode 135 Candy(贪心算法)

    135. Candy There are N children standing in a line. Each child is assigned a rating value. You are g ...

随机推荐

  1. 40个UI设计工具

    摘要:用户界面设计在持续的基础上不断成长和演变.要跟上时代,你需要关注趋势.新资源和正被实施和谈论的新技术. 导读:用户界面设计在持续的基础上不断成长和演变.要跟上时代,你需要关注趋势.新资源和正被实 ...

  2. Hessian和Burlap入门教程

    一.简介 Hessian和Burlap是由Caucho Technology提供的基于HTTP协议的轻量级远程服务解决方案.他们都致力于借助尽可能简单那的API和通信协议来简化Web服务.    He ...

  3. Android学习笔记__2__Android工程目录结构

    一.创建Android工程HelloWorld . src 文件夹里的是源文件 . Android2.2 是引用的类库,这些和 java 的都一样 . gen里面的类就是 ADT 自动生成的啦,一般只 ...

  4. PHP设计模式笔记一:准备工作 -- Rango韩老师 http://www.imooc.com/learn/236

    一.编程字体选择 1.选择等宽字体 包括Courier New ,Consolas,Source Code Pro(推荐) 2.环境搭建(建议easyPHP) 二.开发符合PSR规范的基础框架 PSR ...

  5. ubuntu 配置jdk

    shawn@e014-anle-lnx:~$ sudo su # chmod 777 jdk-6u27-linux-i586.bin # ./jdk-6u27-linux-i586.bin # mv ...

  6. JSTL 格式化输出 Calendar

    今天遇到一个jstl在页面输出Calendar对象的问题,网上各种百度都说不能直接格式化,但是经过自己的尝试,原来是可以直接格式化的. 做个备忘吧. 对象的createTime字段类型是Calenda ...

  7. CCA概述和安装

    什么是CCA? 客户关怀加速器(CCA)为微软动态®CRM通过集中的客户信息从不同的系统在一个集成代理桌面促进剂的效率和有效性. CCA是一个參考应用,利用用户界面集成(UII)为微软Dynamics ...

  8. 4. 绘制光谱曲线QGraphicsView类

    一.前言 Qt的QGraphicsView类具有强大的视图功能,与其一起使用的还有QGraphicsScene类和QGraphicsItem类.大体思路就是通过构建场景类,然后向场景对象中增加各种图元 ...

  9. js keycode 列表

    keycode    8 = BackSpace BackSpace keycode    9 = Tab Tab keycode   12 = Clear keycode   13 = Enter ...

  10. select选项改变时获取选中的option的值

    本来天真的以为直接this.value()就能去到select的值,可事实并非那么回事. <script> document.getElementById('select').onchan ...