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 ...
随机推荐
- springboot EnableAutoConfiguration
http://blog.javachen.com/2016/02/19/spring-boot-auto-configuration.html 自动配置 在启动类上使用@EnableAutoConfi ...
- HTTP协议详解之基本认证篇
•什么是HTTP基本认证: 桌面应用程序也通过HTTP协议跟web服务器交互,桌面应用程序一般不会使用cookie,而是把‘用户名+:+密码’用base64编码之后的string放在request中的 ...
- centos7 卸载 gitlab
标黑的就是关键命令,先停止gitlab服务,然后rpm -e卸载,然后查看剩余gitlab进程,然后杀死主进程,然后删除所有相关目录 1 [liuyx@MiWiFi-R3L-srv ~]$ sudo ...
- WeUI 是由微信官方设计团队专为微信移动 Web 应用设计的 UI 库
WeUI 是由微信官方设计团队专为微信移动 Web 应用设计的 UI 库. WeUI 是一套同微信原生视觉体验一致的基础样式库,为微信 Web 开发量身设计,可以令用户的使用感知更加统一.包含butt ...
- getsockopt和accept需要注意的两个细节
1,getsockopt连续调用问题 通常情况下,在一个socket fd上出现错误时,我们会通过 int status; socklen_t slen; getsockopt(fd, SOL_SOC ...
- cout printf 莫明奇妙的崩溃问题
出现异常主要表现 导致异常的关键代码不是因为printf 或cout,而是因为使用栈空间超出的原因 下图试图在栈上分配1024000个char的空间,确发现崩溃 的位置是printf,这就是这个问题难 ...
- HDUOJ----Good Luck in CET-4 Everybody!
Good Luck in CET-4 Everybody! Time Limit : 1000/1000ms (Java/Other) Memory Limit : 32768/32768K (J ...
- JavaScript 设计模式之代理模式
一.代理模式概念解读 1.代理模式概念文字解读 代理,顾名思义就是帮助别人做事,GOF对代理模式的定义如下: 代理模式(Proxy),为其他对象提供一种代理以控制对这个对象的访问.代理模式使得代理对象 ...
- HDU1024 Max Sum Plus Plus 【DP】
Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- Hive查看table在HDFS上的存储路径
hive>show databases;hive>use databasename;hive>show create table tablename; --查看table的存储路径h ...