# 135-分糖

Description
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(int[] ratings) {
int len = ratings.length;
int[] leftToRight = new int[len];
int[] rightToLeft = new int[len];

Arrays.fill(leftToRight, 1);
Arrays.fill(rightToLeft, 1);

for(int i = 1;i < len;i++){
if(ratings[i] > ratings[i - 1]) leftToRight[i] = leftToRight[i - 1] + 1;
}
for(int i = len - 2;i >= 0;i--){
if(ratings[i] > ratings[i + 1]) rightToLeft[i] = rightToLeft[i + 1] + 1;
}

int sum = 0;

for(int i = 0;i < len;i++)  sum += Math.max(leftToRight[i], rightToLeft[i]);

return sum;
}
}

class Solution {
public int candy(int[] ratings) {
int len = ratings.length;
int[] dp = new int[len];

Arrays.fill(dp, 1);

for(int i = 1;i < len;i++){
if(ratings[i] > ratings[i - 1]) dp[i] = dp[i - 1] + 1;
}

int sum = dp[len - 1];

for(int i = len - 2;i >= 0;i--){
if(ratings[i] > ratings[i + 1]){
dp[i] = Math.max(dp[i], dp[i + 1] + 1);
}
sum += dp[i];
}

return sum;
}
}