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

 1 public class Solution {
 2     public int candy(int[] ratings) {
 3         if (ratings == null || ratings.length < 1)
 4             return 0;
 5         int length = ratings.length;
 6         if (length == 1)
 7             return 1;
 8         int[] candyNum = new int[length];
 9         candyNum[0] = 1;
10         int result = 0;
11         int index = 1;
12         // First, process ascending sub-array
13         while (index < length) {
14             if (ratings[index] > ratings[index - 1])
15                 candyNum[index] = candyNum[index - 1] + 1;
16             else
17                 candyNum[index] = 1;
18             index++;
19         }
20
21         // Then, process descending sub-array
22         index = length - 2;
23         result = candyNum[length - 1];
24         while (index >= 0) {
25             if (ratings[index] > ratings[index + 1])
26                 // Here, we need compare
27                 candyNum[index] = Math.max(candyNum[index + 1] + 1, candyNum[index]);
28             result += candyNum[index];
29             index--;
30         }
31
32         return result;
33     }
34 }
时间: 2024-08-11 09:53:14

Candy 解答的相关文章

PHP常见问题及解答

当作PHP学习时,总是会在baidu上查很多的例如开发环境的选择呀,PHP好不好呀!或者是不是转学JAVA,或是.NET等: 首先本人是从2010年下半年开始报名学的PHP(IN Guangzhou),每周一天学了近6个月左右,从最基础的HTML,CSS,DIV,JAVASCRIPT,AJAX,PHP,然后学二次开发:闲暇之余还开通了一个个人blog( PHP wordpress); 由于个人工作原因,这几年放了一段时间未动PHP了,今年开始又自学了.NET; ---目的就想业余做一份兼职,锻炼

微信送礼物投票系统的详细解答

就目前来说,市场上的第三方微信投票系统种类很多,功能不一鱼龙混杂,功能很多的情况下有一些细微的区别,对于用户来说选择有一定的难度,下面我就来简单介绍一下微信投票活动大家经常关注的16个问题,对此进行详细的解答:只要能同时包括这个些功能的系统,通常都能够很好的满足活动举办方的要求,活动良好的用户体验! Q1.该投票系统可以设置每个微信用户投票次数吗? A3:可以的,可以设置一次活动每个微信用户的投票数,可设置每个微信用户每天的投票数!并且取消关注自动减掉此用户投票的所有记录,做到了自动减票的功能.

2014马哥Linux0217中对0214三题的解答

前几天在做2014马哥Linux0214的作业的时候,发现其实这三题在0217中有解答,当然觉得马哥比自己写得好太多,所以忍不住要把马哥的答案贴出来,以供自己学习. 第一题:写一个脚本,用for循环实现显示/etc/init.d/functions./etc/rc.d/rc.sysinit./etc/fstab有多少行 #!/bin/bash for fileName in /etc/init.d/functions /etc/rc.d/rc.sysinit /etc/fstab;do line

JAVA常见面试题及解答-java开发

JAVA常见面试题及解答 Java的垃圾回收总结  浅谈Java中的内部类 1)transient和volatile是java关键字吗? 如果用transient声明一个实例变量,当对象存储时,它的值不需要维持.例如: class T { transient int a;  //不需要维持 int b;  //需要维持 } 这里,如果T类的一个对象写入一个持久的存储区域,a的内容不被保存,但b的将被保存. volatile修饰符告诉编译器被volatile修饰的变量可以被程序的其他部分改变.在多

[LeetCode][Java] Candy

题目: 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 cand

软件工程之路—解答数据流图

数据流图的做法其实很简单,题的类型有很多,但是万变不离其宗,我们先看一下考试的类型: 1.确定实体(数据源)名称 2.确定数据文件(数据存储)的名称 3.确定加工的名称 4,.找出缺少的数据流 其实,这就是在考查,数据流图的画法. 也就是说,根据题中系统的系列描述,能够画出数据流图,那么,这题能拿满分就是板上钉钉的事儿. 首先看一下一般流程图的画法 (1)首先画系统的输入输出,即先画顶层数据流图.顶层流图只包含一个加工,用以表示被开发的系统,然后考虑该系统有哪些输入数据.输出数据流.顶层图的作用

在axure中实现商品数量加减效果,原型库网站讲师-金乌 解答同学问

有同学在群里提问,如何使用axure制作商品数量增加减少效果,见GIF图.虽然属于初级教程,但很多同学还是小白新手阶段,所以特地录制了详细的视频讲解,供大家学习参考! 该教程由原型库网站录制http://www.yuanxingku.com转载请注明出处! 在axure中实现商品数量加减效果,原型库网站讲师-金乌 解答同学问,布布扣,bubuko.com

解答zabbix在configure时候遇到的问题(CentOS)

zabbix在configure时候遇到的问题(CentOS)为你解答: 在CentOS系统中,安装zabbix进行configure时会遇到以下4个主要问题 ./configure --enable-server --enable-agent --with-mysql --with-net-snmp --with-jabber --with-libcurl 1 configure: error: MySQL library not found the problem is not instal

C++ Primer 第四版课后练习解答 习题1.1

注意:本随笔是在<C++Primer(第四版)习题解答(完整版)>中直接抄录的.此处主要是便于本人以后反复阅读. 习题1.1 查看所用的编译器文档,了解它所用的文件命名规范.编译并运行本节的main程序. [解答] 一般而言,C++编译器要求编译的程序保存在文件中.C++程序一般涉及两类文件:头文件和源文件.大多数系统中,文件的名字由文件名和文件后缀(又称扩展名)组成.文件后缀通常表明文件的类型,如头文件的后缀可以是.h或.hpp等:源文件和后缀可以是.cc或.cpp等,具体的后缀与使用的编译