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 ...
随机推荐
- 推断php操作mysql(添删改查)是否成功
近期在使用CI框架 , 可是里面的数据库操作没有ThinkPhp方便 , 不知道数据库操作的反馈信息 , 仅仅好借助原生方法来推断是否操作数据库成功 推断php操作mysql(添删改查)是否成功,主要 ...
- 获取Android系统应用信息
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- sjtu oj 1201. SuperXOR
Description Pangzi recently realized that bitwise XOR operation is just an addition without carries. ...
- JavaScript SHA-1
1. [文件] webtoolkit.sha1.js ~ 4KB /**** Secure Hash Algorithm (SHA1)* http://www.huiyi8.com/css ...
- oracle:ora-12560 tns 协议适配器错误
今天新安装了一个oracle server,实例启动了,监听状态也正常. [oracle@db ~]$ lsnrctl status LSNRCTL for Linux: Version 11.2.0 ...
- poj 2185 Milking Grid(next数组求最小循环节)
题意:求最小的循环矩形 思路:分别求出行.列的最小循环节,乘积即可. #include<iostream> #include<stdio.h> #include<stri ...
- skynet源码阅读<1>--lua与c的基本交互
阅读skynet的lua-c交互部分代码时,可以看到如下处理: struct skynet_context * context = lua_touserdata(L, lua_upvalueindex ...
- 用 SDL2 平铺背景并显示前景
环境:SDL2 + VC++2015 下面的代码将打开background.bmp和image.bmp,将background平铺背景,将image作为前景呈现 #include <iostre ...
- bzoj3998
后缀自动机+dp 想了挺长时间 后缀自动机的状态图是一个dag,从root走到一个点的路径数代表了这个状态包含的子串,我们先预处理出来每个节点向后走能够形成多少子串,注意这里不是直接在parent树上 ...
- hdoj3183【思维】
思路: 处理方案非常霸气啊,无奈想不到. 说是n位去m个,那么默认就是取了n-m个数字,ok,然后m #include <iostream> #include <stdio.h> ...