LeetCode: Candy 解题报告
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 解题报告的更多相关文章
- LeetCode: Permutations 解题报告
Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...
- 【原创】leetCodeOj --- Candy 解题报告
题目地址: https://leetcode.com/problems/candy/ 题目内容: Candy Total Accepted: 43150 Total Submissions: 2038 ...
- leetcode—Palindrome 解题报告
1.题目描述 Given a string s, partition s such that every substring of the partition is a palindrome. Ret ...
- LeetCode C++ 解题报告
自己做得LeetCode的题解,使用C++语言. 说明:大多数自己做得,部分参考别人的思路,仅供参考; GitHub地址:https://github.com/amazingyyc/The-Solut ...
- C++版 - 剑指offer之面试题37:两个链表的第一个公共结点[LeetCode 160] 解题报告
剑指offer之面试题37 两个链表的第一个公共结点 提交网址: http://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?t ...
- codeforces 436A. Feed with Candy 解题报告
题目链接:http://codeforces.com/contest/436/problem/A 题目意思:给出 n 颗只有两种类型:fruit 和 caramel的candies,这些candies ...
- UVALive 5791 Candy's Candy 解题报告
比赛总结 题目 题意: 有f种口味的糖果,现在要把每颗糖果分到一些packs里面去.packs分两种: flavored pack:只有一种口味. variety pack:每种口味都有. 求满足下列 ...
- LeetCode: Subsets 解题报告
Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset ...
- LeetCode: Triangle 解题报告
Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to a ...
随机推荐
- 14、Java中用浮点型数据Float和Double进行精确计算时的精度问题
一.浮点计算中发生精度丢失 大概很多有编程经验的朋友都对这个问题不陌生了:无论你使用的是什么编程语言,在使用浮点型数据进行精确计算时,你都有可能遇到计算结果出错的情况.来看下面的例子. // 这是一个 ...
- 获取List对象的泛型类(原创)
群里一个伙计的需求,最后我提出了这种解决方案,不过他觉得多写俩括号增加了调用方的难度.还是先记下来吧,有时间看看还能不能再改造. 1.直接获取时获取不到的,类型被虚拟机擦除了2.利用子类实现父类的 ...
- JNI 在命令行窗口输入字符,不显所输入字符,显指定的掩饰符
//JNI-命令行窗口输入字符,显掩饰符.txt /* 目标:在命令行窗口输入字符,不显所输入字符,显指定的掩饰符 作者:tangshancheng@21cn.com*/ 1.KeyBoard.j ...
- servlet下根据相对路径找资源
1.在web项目中如果直接添加一个资源,那么相对路径相对的是tomcat的bin目录. 2.在包中直接指定资源,那么可以使用以下的相对路径直接获取资源: InputStream in = this.g ...
- Emacs 文件中的查找操作
1,在本文件中查找 list-matching-lines 命令会列出本文件中所有出现text的地方.下面是它的一个输出示例: 7 matches for "ngx_http_wait_re ...
- nyoj------擅长排列的小明
擅长排列的小明 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 小明十分聪明,而且十分擅长排列计算.比如给小明一个数字5,他能立刻给出1-5按字典序 ...
- 【LeetCode】104. Maximum Depth of Binary Tree (2 solutions)
Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is the ...
- 解决错误This message can only be sent over HTTPS.
在做基于Owin oauth客户端申请授权,调用如下代码: this.InitializeWebServerClient(); var userAuthorization = _webServerCl ...
- 数据源与JNDI资源实现JSP数据库连接池实例
名词解释:JNDI的全称是java命名与目录接口(Java Naming and Directory Interface),是一个应用程序设计的API,为开发人员提供了查找和访问各种命名和目录服务的通 ...
- SELECT控件add方法 ie 类型不匹配
s = document.createElement('select'); try{ //for ie8 or earlier s.add(new Option('text','value'),s.o ...