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. Hdu5737-Differencia(有序表线段树)

    题意很直观,我就不说了. 解析:这是我以前没有接触过的线段树类型,有序表线段树,每个节点申请了两段空间,主要是为了保存左边儿子会有多少比v小的,右边儿子会有多少比v小 的,所以在建树过程中要归并排序. ...

  2. javascript 我是广告

    <!DOCTYPE html><html><head><meta charset="utf-8"><meta http-equ ...

  3. Direct3D 纹理映射

    纹理映射是将2D的图片映射到一个3D物体上面,物体上漂亮图案被称为纹理贴图, 一个表面可以支持多张贴图等等,下面简单介绍下纹理贴图 纹理贴图UV: 贴图是一个个像素点组成,每一个像素点都由一个坐标最后 ...

  4. qt模型学习

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' from PyQt4.Qt import * from PyQt4. ...

  5. qt Graphics View Framework(非重点)

    Graphics View 提供了一种接口,用于管理大量自定义的 2D 图形元素,并与之进行交互:还提供了用于将这些元素进行可视化显示的观察组件,并支持缩放和旋转. 说明;Graphics View ...

  6. sql求和涉及到null值

    SQL ISNULL().NVL().IFNULL() 和 COALESCE() 函数 请看下面的 "Products" 表: P_Id ProductName UnitPrice ...

  7. AJAX实现google搜索建议实战

    搜索建议实战的目标是为了输入搜索内容,动态的进行匹配,效果图如下: 整体思路: 在客户端搜索框中触发onkeyup事件, 随时向PHP服务器请求当前输入框中的内容, PHP服务器获取到keywords ...

  8. hdu 5063 Operation the Sequence(Bestcoder Round #13)

    Operation the Sequence                                                                     Time Limi ...

  9. python -序列化

    python中用于序列化的两个模块 json 用于[字符串]和[python基本数据类型]之间进行转换 pickle 用于[python特有的类型] 和 [python基本数据类型]间进行转换 Jso ...

  10. javascript的本地存储 cookies、localStorage

    一.cookies 本地存储 首先是常用的cookies方法,网上有很多相关的代码以及w3cSchool cookies. // 存储cookies function setCookie(name,v ...