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 candies than their neighbors.
What is the minimum candies you must give?

Hide Tags

SOLUTION:

使用一个数组记录每一个小孩应得的糖果的数目

1.我们可以从左往右扫描,如果遇到上升区间,就给小孩比左边多一个糖,否则就给1个糖果。

2.我们可以从右往左扫描,如果遇到上升区间,就给小孩比右边多一个糖,否则就给1个糖果。

同时,应该与1步算出的值取一个大值(因为有可能要给小孩更多糖果才可以满足题设)。

 public class Solution {
public int candy(int[] ratings) {
if (ratings == null || ratings.length == ) {
return ;
} int len = ratings.length;
int[] num = new int[len]; // go from left to right;
for (int i = ; i < len; i++) {
if (i > && ratings[i] > ratings[i - ]) {
num[i] = num[i - ] + ;
} else {
num[i] = ;
}
} // go from right to left;
int sum = ;
for (int i = len - ; i >= ; i--) {
if (i < len - && ratings[i] > ratings[i + ]) {
num[i] = Math.max(num[i], num[i + ] + );
}
sum += num[i];
} return sum;
}
}

CODE:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/dp/Candy.java

LeetCode: Candy 解题报告的更多相关文章

  1. LeetCode: Permutations 解题报告

    Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...

  2. 【原创】leetCodeOj --- Candy 解题报告

    题目地址: https://leetcode.com/problems/candy/ 题目内容: Candy Total Accepted: 43150 Total Submissions: 2038 ...

  3. leetcode—Palindrome 解题报告

    1.题目描述 Given a string s, partition s such that every substring of the partition is a palindrome. Ret ...

  4. LeetCode C++ 解题报告

    自己做得LeetCode的题解,使用C++语言. 说明:大多数自己做得,部分参考别人的思路,仅供参考; GitHub地址:https://github.com/amazingyyc/The-Solut ...

  5. C++版 - 剑指offer之面试题37:两个链表的第一个公共结点[LeetCode 160] 解题报告

    剑指offer之面试题37 两个链表的第一个公共结点 提交网址: http://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?t ...

  6. codeforces 436A. Feed with Candy 解题报告

    题目链接:http://codeforces.com/contest/436/problem/A 题目意思:给出 n 颗只有两种类型:fruit 和 caramel的candies,这些candies ...

  7. UVALive 5791 Candy's Candy 解题报告

    比赛总结 题目 题意: 有f种口味的糖果,现在要把每颗糖果分到一些packs里面去.packs分两种: flavored pack:只有一种口味. variety pack:每种口味都有. 求满足下列 ...

  8. LeetCode: Subsets 解题报告

    Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset ...

  9. LeetCode: Triangle 解题报告

    Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to a ...

随机推荐

  1. [Done]Spring @Pointcut 切点调用不到(SpringAOP嵌套方法不起作用) 注意事项

    今天在开发过程中,遇到一个问题卡了很久,测试代码如下: package spring.pointcut; import org.aspectj.lang.ProceedingJoinPoint; im ...

  2. c# 判断端口是否被占用

    这里主要用到:命名空间System.Net.NetworkInformation下定义了一个名为IPGlobalProperties的类 具体代码 class PortHelper { #region ...

  3. 分享一个php代码创建目录的Demo

    /* * 连续建目录 * string $dir 目录字符串 * int $mode 权限数字 * 返回:顺利创建或者全部已建返回true,其它方式返回false */ function makeDi ...

  4. js 取值 getElementsByTagName,getElementsByName

    getElementsByTagName,getElementsByName  获取的值是数组的所以用[0][1]引用 1 <select multiple size="2" ...

  5. HDUOJ---The number of divisors(约数) about Humble Numbers

    The number of divisors(约数) about Humble Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Lim ...

  6. C 应用

    前言 1)操作符两端必须加空格,(每行第一个赋值语句对齐). 2)变量名必须是英文(不能是拼音):英文.数字.下划线和美元符号. 3)等于号 == 反过来写(0 == i%4)防止少些赋值号的错误. ...

  7. OkHttp拦截器的实现原理

    今天项目中遇到需要将从push接收到的数据按照协议parse成应用层需要的结构化数据类型问题:因为push消息类型繁多,等待解析出的结构化数据类型也多样,有的还需要经过几步的parse过程:而且因为项 ...

  8. C 标准IO 库函数与Unbuffered IO函数

    先来看看C标准I/O库函数是如何用系统调用实现的. fopen(3) 调用open(2)打开指定的文件,返回一个文件描述符(就是一个int 类型的编号),分配一 个FILE 结构体, 通常里面包含了: ...

  9. Appium測试安卓Launcher以滑动窗口获得目标应用

    所谓Launcher,指的是安卓的桌面管理程序,全部的应用图标都放在launcher上面.事实上这是一个非常easy的样例,仅仅是为了验证几点想法而已. 1.实验目的 做这个试验的目的有二 尝试下窗口 ...

  10. python列表推导式详解 列表推导式详解 字典推导式 详解 集合推导式详解 嵌套列表推导式详解

    推导式是Python中很强大的.很受欢迎的特性,具有语言简洁,简化代码,速度快等优点.推导式包括:1.列表推导式2.字典推导式3.集合推导式4.嵌套列表推导式注意: 字典和集合推导是最近才加入到Pyt ...