###### [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?

class Solution {
public:
int candy(vector<int>& ratings) {
int size = ratings.size();
if(size <= 1)return ratings.size();
vector<int> nums(size, 1);
for(int i = 1; i < size; i++) {
if(ratings[i] > ratings[i-1])
nums[i]  = nums[i-1] + 1;
}
for(int i = size - 1; i > 0; i--) {
if(ratings[i-1] > ratings[i])
nums[i-1] = max(nums[i-1], nums[i] + 1);
}
int result = 0;
for(int i = 0; i < size; i++)
result += nums[i];
return result;
}
};

#### LeetCode(135) Candy

2015-01-20 10:40:23

#### [leetcode-135]Candy(java)

2015-08-14 17:09:17

#### [LeetCode]135.Candy

2014-01-25 16:39:03

#### leetcode 135.Candy

2016-09-28 20:52:52

#### 【LeetCode】135.Candy(hard)解题报告

2017-11-06 19:13:26

#### 135.Candy

2015-07-01 21:02:56

#### 刷LeetCode刷LeetCode刷LeetCode刷LeetCode

2018年01月09日 1.55MB 下载

#### leetcode试题的答案

2018年01月14日 101KB 下载

#### leetbook书籍

2018年03月04日 1.26MB 下载

#### 算法刷题LeetCode

2018年01月08日 837KB 下载

## 不良信息举报

[leetcode] 135.Candy