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 ...
随机推荐
- Swift教程_swift常见问题(0005)_完美解决Cannot override 'dealloc'异常
Swift教程_swift常见问题(0001)_CoreData: warning: Unable to load class named 'xxx' for entity 'xxx' Swift教程 ...
- 30、java中递归算法
1.已知有一个数列f(0)=1,f(1)=4,f(n+2)=2*f(n+1)+f(n),其中n是大于0的正数,求f(10)的值. 分析:设x=n+2 => f(x)=2*f(n-1)+f(n-2 ...
- java Socket Tcp示例三则(服务端处理数据、上传文件)
示例一: package cn.itcast.net.p5.tcptest; import java.io.BufferedReader;import java.io.IOException;impo ...
- CentOS6.3的VNC--远程桌面
2G内存的服务器开启Gnome图形化界面应该没什么问题.1G还有512M的内存的就不敢开启了,现在内存正常状态就已经60%左右了. CentOS6.3服务器,Gnome图形化界面按照阿里官方步骤:一. ...
- shell随机读取一行
使用shell随机读取文件的一行数据 shuf -n1 file_name
- 基于Arch的live系统
今天在linuxsir的archlinux分区闲逛,看到了carbonjiao的帖子 http://bbs.linuxeye.cn/thread-652-1-1.html 同时还关注他的帖子:1.Ar ...
- PHP API中,MYSQL与MYSQLI的持久连接区别
转载自:http://www.cnxct.com/some-differences-between-mysql-and-mysqli-of-persistent-connection/ 很久很久以前, ...
- HOJ 1402 整数划分
HOJ1402 整数划分 http://acm.hit.edu.cn/hoj/problem/view?id=1402 [题目描述] 整数划分是一个经典的问题.希望这道题会对你的组合数学的解题能力有所 ...
- Google的创新九原则(转)
原文url:http://www.365xiaoxi.com/All/News/2013-11-22/6432.html 想知道是什么让Google成为生产力与创造力的圣杯?当然不是喝山景城脚下的神水 ...
- python练习笔记——完全数(1000以内的)
完全数(Perfect number),又称完美数或完备数,是一些特殊的自然数.它所有的真因子(即除了自身以外的约数)的和(即因子函数),恰好等于它本身.如果一个数恰好等于它的因子之和,则称该数为“完 ...