There is N children standing in a line. Each child is assigned a rating value.
You is giving candies to these children subjected to the following requirements:
- Each child must has at least one candy.
- Children with a higher rating get more candies than their neighbors.
What's the minimum candies you must give?
In the process of iterating over an array, it can be roughly divided into three cases: 1. The latter child scored higher than the previous child; 2. The same score; 3. The score is lower than the previous child.
In the first case, the number of sweets is more than +1 of the number of sweets the previous child received. For example c[i-1] give x candy, c[i] should give x+1 candy. Here a variable curcandy is required to record the number of sweets before a child x.
In the second case, according to the title, only the children of higher rating can get more sweets than the people around them, so if the scores are the same, they will be distributed randomly. To keep the total as small as possible, the newly traversed child should be given 1 sweets (total candy number +1).
The third case is more complicated. Here we use an example to illustrate:
Suppose your child's score is in the following order:
Ratings:1 2 5 4 3 2 1
Index:0 1 2 3 4 5 6
Candy[i] Indicates the increment of the total number of candies that should be allocated after traversing to child I
According to the situation 1,candy[0]=1, candy[1]=2, candy[2]=3
Number 3rd when the child came, because the score is less than the second child, in order to minimize the total, should be divided to 3rd children 1 sweets, candy[3]=1
When the 4th child came, it was supposed to be less candy than the number 3rd, but the number 3rd had only 1, so it was necessary to adjust the 3rd number of children's sweets, to get 3rd to take one more, then 4th to take 1 sweets. The question is, does 3rd take 1 sweets, will there be conflict with number 2nd? Here, number 2nd takes 3 sweets, and if 3rd takes 1 more (i.e. 2 sweets), there will be no conflict.
So candy[4]=2 (4th, take 1 sweets, 3rd replacement 1 sweets)
When the 5th child came, the same thing to give 3rd and 4th to replace the candy, but so the 3rd candy on the 3, and 2nd, which is not allowed, so we have to pay 2nd to reissue another candy.
So Candy[5]=4 (5th, take 1 sweets, 2,3,4 each replacement of a candy).
Similarly candy[6]=5
In the process of analyzing the third case, we need to know this information: 1. Homeopathic to the front child to replace the candy, to which child until the replacement? 2. When you need to give more than one child to replace the candy (in the example, when traversing to number 5th, you need to give the number 2nd to replace the candy, but no need to traverse to number 4th)
For question 1, the answer is to replace the most recent one with a higher or equal number of children (peak) than the previous child, which is recorded as top. For question 2, if top is the same candy as the latter, it needs to be replaced with a more top.
So it takes two variables, index record top subscript, capacity record top to get candy.
The increment can be simply expressed in i-index or i-index+1, such as Candy[3]=1, when index is 2, this 1 can be calculated with I-index (i.e.: 3-2).
Similarly candy[4]= i-index = 4-2 = 2
CANDY[5] should have been equal to i-index=3. But capacity for 3,i-index=capacity means more replacement for one child, so candy[5]=i-index+1=4
Similarly candy[6]=5
Then the total number of candies is sum (candy[0~6])
Finally, it should be noted that if you encounter the same ratings child, Index will also need to move to the last child, capacity record is also the last child to take the number of sweets.
The code is as follows:
1 Public intCandyint[] ratings) {2 intRe = 1;3 intCurcandy = 1;4 intindex = 0;5 intCapacity = 1;6 for(inti=1;i<ratings.length;i++) {7 if(ratings[i]>ratings[i-1]) {8index =i;9curcandy++;TenCapacity =Curcandy; OneRe + =Curcandy; A } - Else if(ratings[i]==ratings[i-1]) { -index=i; theRe + = 1; -Curcandy=1; -Capacity=1; - } + Else if((I-index) <capacity) { -Re + =-index; +Curcandy=1; A } at Else { -Re + = I-index+1; -Curcandy=1; - } - } - returnre; in}
[Leetcode] [JAVA] Candy