leetcode-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?

public class Solution {
public int candy(int[] ratings) {
if(ratings.length == 1){
return 1;
}
int[] candys = new int[ratings.length];
candys[0] = 1;
for(int i = 1; i < ratings.length; i++){
if(ratings[i] > ratings[i - 1]){
candys[i] = candys[i - 1] + 1;
}else{
candys[i] = 1;
}
}
for(int i = ratings.length - 2; i >= 0; i--){
if(ratings[i] > ratings[i + 1] && candys[i] <= candys[i + 1]){
candys[i] = candys[i + 1] + 1;
}
}
int sum = 0;
for(int x : candys){
sum += x;
}
return sum;
}
}

LeetCode-135. Candy (JAVA)根据等级分糖果

2017-04-11 13:08:38

聊天室Candy-chat 部署（Candy— a JavaScript-based multi-user chat client）

2015-04-17 14:37:41

【LeetCode】Candy 解题报告

2014-09-04 22:46:47

135. candy 贪心算法

2016-09-13 20:29:38

UVALive 5791 Candy's Candy 解题报告

2013-09-01 20:44:32

Candy -- LeetCode

2014-03-18 01:44:27

Alien Skin Eye Candy v7 for Mac(PS滤镜插件) v7.2.0.50破解版

2018-01-07 21:23:39

eye candy 4000 Demo

2011年05月21日 962KB 下载

Candy研究笔记-xmpp

2012-04-05 14:37:23

135.Candy

2015-07-01 21:02:56

不良信息举报

leetcode-135. Candy