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 ...
随机推荐
- 全国出现大面积DNS服务器故障 域名被劫持
1月21日消息,继今日上午腾讯16项服务出现故障后,大量网站出现了无法访问的情况,据了解,该故障是由于国内DNS根服务器故障所致. 据了解,此次攻击式由于国内所有通用顶级域的根服务器出现异常,导致大量 ...
- 【BIEE】导出数据报错
使用BIEE导出数据的时候,发现个问题,导出过程中,报错如下: 问题解决: 找到文件opmn.xml,路径为:/Middleware/instances/instance1/config/OPMN/o ...
- 【shell】正则表达式
当一个文件或命令输出中抽取或过滤文本时,可以使用正则表达式(RE),正则表达式是一些特殊或很不特殊的字符串模式的集合. 在Linux中grep.awk.sed均可解释正则 1.基本元字符集及其定义 ^ ...
- windows下编辑的shell复制到linux无法执行
是因为格式不对 可用vim编辑器转换格式 在vim中执行命令: set ff=unix 设置打开方式为unix
- JavaScript的技巧和最佳实践
JavaScript是一个绝冠全球的编程语言,可用于Web开发.移动应用开发(PhoneGap.Appcelerator).服务器端开发 (Node.js和Wakanda)等等.JavaScript还 ...
- MySQL5.7 二进制源码包安装
一般平时安装MySQL都是源码包安装的,但是由于它的编译需要很长的时间,所以建议安装二进制免编译包.可以到MySQL官方网站去下载,也可以到comsenz官方网站下载,还有各大镜像站下载. 下载安装包 ...
- [转载]打造自己喜欢的Linux桌面----archlinux
原文地址:打造自己喜欢的Linux桌面----archlinux作者:三尺椴 打造自己的Linux桌面----Archlinux 2011-01-16 文/s_cd ( 常用桌面组合:Archlin ...
- HDUOJ------敌兵布阵
敌兵布阵 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submissi ...
- C# 添加Windows服务,定时任务
源码下载地址:http://files.cnblogs.com/files/lanyubaicl/20160830Windows%E6%9C%8D%E5%8A%A1.zip 步骤 一 . 创建服务项目 ...
- Redis的Docker镜像
原文地址:https://hub.docker.com/_/redis/ Pull Command docker pull redis Short Description Redis is an op ...