题目:https://leetcode-cn.com/problems/candy/
简单分析:从前往后扫描,从后往前扫描。向左看看,我比他分高,我就要比他多:向右看看,我比他分高,我就要比他多。多一个也行,小孩子不能贪。
class Solution {
public int candy(int[] ratings) {
int n =ratings.length;
if(n==0) return 0;
int[] array = new int[n];
array[0] = 1;
for(int i=1;i<n ; i++){
array[i] = 1;
if(ratings[i]>ratings[i-1]){
array[i] = array[i-1] +1;
}
}
for(int i=n-1;i>0;i--){
if(ratings[i-1]>ratings[i]){
array[i-1] = Math.max(array[i]+1,array[i-1]);
}
}
int sum = 0;
for(int i=0; i<n; i++){
sum = sum + array[i];
}
return sum;
}
}