135 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?
详见:https://leetcode.com/problems/candy/description/
Java实现:
首先初始化每个人一个糖果,然后遍历两遍,第一遍从左向右遍历,如果右边小盆友的等级高,右边小盆友加一个糖果,这样保证了一个方向上高等级的糖果多。然后再从右向左遍历一遍,如果相邻两个左边的等级高,而左边的糖果又少的话,则左边糖果数为右边糖果数加一。最后再把所有小盆友的糖果数都加起来返回即可。
class Solution {
public int candy(int[] ratings) {
int n=ratings.length;
if(n==0||ratings==null){
return 0;
}
int[] dp=new int[n];
Arrays.fill(dp,1);
int candy=0;
for(int i=1;i<n;++i){
if(ratings[i]>ratings[i-1]){
dp[i]=dp[i-1]+1;
}
}
for(int i=n-2;i>=0;--i){
if(ratings[i]>ratings[i+1]){
dp[i]=Math.max(dp[i],dp[i+1]+1);
}
}
for(int val:dp){
candy+=val;
}
return candy;
}
}
参考:https://www.cnblogs.com/grandyang/p/4575026.html
135 Candy 分配糖果的更多相关文章
- LeetCode 135 Candy(贪心算法)
135. Candy There are N children standing in a line. Each child is assigned a rating value. You are g ...
- (LeetCode 135) Candy N个孩子站成一排,给每个人设定一个权重
原文:http://www.cnblogs.com/AndyJee/p/4483043.html There are N children standing in a line. Each child ...
- [LeetCode] Candy (分糖果),时间复杂度O(n),空间复杂度为O(1),且只需遍历一次的实现
[LeetCode] Candy (分糖果),时间复杂度O(n),空间复杂度为O(1),且只需遍历一次的实现 原题: There are N children standing in a line. ...
- leetcode 135. Candy ----- java
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- [LeetCode] Candy 分糖果问题
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- Leetcode#135 Candy
原题地址 遍历所有小孩的分数 1. 若小孩的分数递增,分给小孩的糖果依次+12. 若小孩的分数递减,分给小孩的糖果依次-13. 若小孩的分数相等,分给小孩的糖果设为1 当递减序列结束时,如果少分了糖果 ...
- [LeetCode] Candy Crush 糖果消消乐
This question is about implementing a basic elimination algorithm for Candy Crush. Given a 2D intege ...
- [leet code 135]candy
1 题目 There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- LeetCode OJ:Candy(糖果问题)
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
随机推荐
- Why there are no job running on hadoop
Using hadoop1.3.0. I ran the example WordCount correctly in eclipse. But when I enter localhost:5003 ...
- How do I set the timeout for a JAX-WS webservice client?
How do I set the timeout for a JAX-WS webservice client? up vote58down votefavorite 27 I've used JAX ...
- IE67 下 setattribute class 失效
解决办法.将class 换成 className ,同理.ff不能识别className,将其换成class element.setAttribute("class"," ...
- HDU4292 Food —— 最大流 + 拆点
题目链接:https://vjudge.net/problem/HDU-4292 Food Time Limit: 2000/1000 MS (Java/Others) Memory Limit ...
- POJ3258 River Hopscotch —— 二分
题目链接:http://poj.org/problem?id=3258 River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total ...
- codeforces 460A Vasya and Socks 解题报告
题目链接:http://codeforces.com/problemset/problem/460/A 题目意思:有一个人有 n 对袜子,每天早上会穿一对,然后当天的晚上就会扔掉,不过他会在 m 的倍 ...
- codeforces 399B. Red and Blue Balls 解题报告
题目链接:http://codeforces.com/problemset/problem/399/B 题目意思:给出 n 个只由 R 和 B 组成的字符串(由上到下排列,相当于栈),问最多可以操作多 ...
- Oracle:imp导入imp-00000问题
现场环境:window2008 . oracle11.2g .客户端安装的是oracle10g一个简洁版 34M的. 在imp导入时,提示 Message 100 not found; No mes ...
- 从BadBoy导入脚本并调试
一. 利用BadBoy录制自动化脚本,录制事件为禅道中创建bug 在badboy地址栏输入被访问的URL地址 录制成功后截图如下: 录制完成后在badboy窗口中回放确定脚本录制的正确性,回放成功后清 ...
- Python中的sort() key含义
sorted(iterable[, cmp[, key[, reverse]]]) iterable.sort(cmp[, key[, reverse]]) 参数解释: (1)iterable指定要排 ...