class Solution {
public int candy(int[] ratings) {
//从左侧和右侧分别扫描一遍,计算当前孩子是否是比两侧的孩子优秀
int length = ratings.length;
int[] candyNum = new int[length];
for (int i = 0; i < length; i++){
candyNum[i] = 1;
}
for (int i = 1; i < length; i++){
if (ratings[i - 1] < ratings[i]){
candyNum[i] = candyNum[i - 1] + 1;
}
}
for (int i = length - 2; i >= 0; i--){
if (ratings[i + 1] < ratings[i] && candyNum[i + 1] >= candyNum[i] ){
candyNum[i] = candyNum[i + 1] + 1;
}
}
int total = 0;
for (int i = 0; i < length; i++){
total += candyNum[i];
}
return total;
}
}
标签:150,ratings,int,++,十六,candyNum,面试,length,total
From: https://www.cnblogs.com/poteitoutou/p/18011983