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 ...
随机推荐
- mvn_action
validate(验证): 验证项目正确,并且所有必要信息可用. compile(编译): 编译项目源码 test(测试): 使用合适的单元测试框架测试编译后的源码. package(打包): 源码编 ...
- Axure Base 10 动态面板滑动效果
示例原型:http://pan.baidu.com/s/1mgjYahi 实现目标: 1. 点击登录滑出登录面板 2. 点击确定滑出动态面板 最终效果如下: 这种效果可以通过两种方法实现: 首先准 ...
- 开启 J2EE(六)— Servlet之Filter具体解释及乱码处理实例
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/akkzhjj/article/details/36471477 定义和功能 Filter:过滤器,它 ...
- ossfs常见配置错误
以下问题出现在非root用户下 执行echo ××××> /etc/passwd-ossfs bash: /etc/passwd-ossfs: Permission denied 使用sudo ...
- Could not load file or assembly 'MyAssembly.XmlSerializers
https://stackoverflow.com/questions/17755559/could-not-load-file-or-assembly-myassembly-xmlserialize ...
- 如何去掉idea里mybatis的.xml文件 sql 语句背景色
点击有背景的地方 Alt+Enter选择 un-inject Language/refence 即可去掉
- SPOJ:Another Version of Inversion(二维数组的逆序对)
DCE Coders admins are way much geekier than they actually seem! Kartik has been following that tradi ...
- nodejs 打造 多人对战游戏服务器(初级入门)
使用socket.set 和 socket.get 在存取玩家信息 百牛信息技术bainiu.ltd整理发布于博客园 socket.get('playerinfo', function (err, p ...
- codeforces 126B
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. ...
- caffe从入门到放弃
断断续续折腾ML近一年,写点博客记录这个坑.