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?
首先初始化每个人一个糖果,然后这个算法需要遍历两遍,第一遍从左向右遍历,如果右边的小盆友的等级高,等加一个糖果,这样保证了一个方向上高等级的糖果多。然后再从右向左遍历一遍,如果相邻两个左边的等级高,而左边的糖果又少的话,则左边糖果数为右边糖果数加一。最后再把所有小盆友的糖果数都加起来返回即可。
Runtime: 5 ms beats 22.22% of java submissions.
public int candy(int[] ratings) {
int size=ratings.length;
if(size<=1) return size;
int[] num=new int[size];
Arrays.fill(num,1);
for (int i = 1; i < size; i++) if(ratings[i]>ratings[i-1]) num[i]=num[i-1]+1;
for (int i= size-1; i>0 ; i--) if(ratings[i-1]>ratings[i]) num[i-1]=Math.max(num[i]+1,num[i-1]);
int result=0;
for (int i = 0; i < size; i++) result+=num[i];
return result;
}