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 ...
随机推荐
- java上下文Context类
Context在Java中的出现是如此频繁,但其中文翻译"上下文"又是如此诡异拗口,因此导致很多人不是很了解Context的具体含义是指什么,所以很有必要来深究一下这词的含义. 先 ...
- Struts2的工作原理(图解)详解
Struts2的工作原理 上图来源于Struts2官方站点,是Struts 2 的整体结构. 一个请求在Struts2框架中的处理大概分为以下几个步骤(可查看源码:https://github.com ...
- 在Android Studio中移除导入的模块依赖
进入settings.gradle(Project Settings) include ':app', ':pull_down_list_view' 要移除的Module dependency为“pu ...
- codeforces 441C. Valera and Tubes 解题报告
题目链接:http://codeforces.com/problemset/problem/441/C 题目意思:将n * m 的矩阵分成 k 堆.每堆是由一些坐标点(x, y)组成的.每堆里面至少由 ...
- 让th里面的东西自动换行
让th里面的东西自动换行 html中的TH里面的文字不管多长,始终是一行,很烦 <th style="word-wrap:break-word;">aaaaaaaaaa ...
- 「LuoguP1799」 数列_NOI导刊2010提高(06)
题目描述 虽然msh长大了,但她还是很喜欢找点游戏自娱自乐.有一天,她在纸上写了一串数字:1,1,2,5,4.接着她擦掉了一个l,结果发现剩下1,2,4都在自己所在的位置上,即1在第1位,2在第2位, ...
- 棋盘问题(dp)
棋盘问题 传送门 题目描述 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个 ...
- noip2010引水入城
https://www.zybuluo.com/ysner/note/1334997 这道题fst了 题面 戳我 解析 我一开始的想法是,按照高度给第一行排序,然后贪心地选取目前到不了的,高度最高的第 ...
- laravel 自定义分页 offset 和 limit 的使用
laravel 本身有一个自带的快速分页方法 paginate,只需要传入每页显示多少条数据就可以 了,但是如果想使用自定义从哪里开始呢,这时候就可以使用offset 和 limit 的组合,offs ...
- vs 2015 community Blend和devenv启动的区别
使用Blend启动会有部分功能无法显示 如:SVN管理插件,工具栏 使用devenv启动会全部显示