Problem
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?
Example 1:
1 |
|
Example 2:
1 |
|
Explanation 1
- We can use two iterations to solve this problem. Init first child have 1 candy. First iteration is from left to right, if the right child has a higher rating, then its number of candy is the left child’s number of candy plus one; else, the right child also has one candy. This iteration is guaranteed that in a one way, the higher rating children has more candy.
- The second iteration is from right to left, if the left child has higher rating and the left child’s number of candy is less or equal to the right child, then the number of candy for the left child is equal to the number of candy for the right child plus one.
- Finally, iterate all the children’s candies and return.
Solution 1
1 |
|
Explanation 2
- We can use only one iteration. Give the first child one candy, consider the following three situations:
- The current child’s rating is equal to the previous child, then the current child have one candy.
- The current child’s rating is greater than the previous child, then the current child have one more candy than the previous child.
- The current child’s rating is less than the previous child, then we don’t know how many candy give to the current child.
-
For the above third situation, if we give 1 candy to the current child, then if the following next child has even less rating, then that child cannot have 0 candy. If we give one less candy (compare to the previous child) to the current child, then if there is no following next child, then we gave more to the current child (we should give one). Also, if there are many next child’s ratings getting lower and lower, then the previous child’s candy may need to increase. (
ratings[1, 100, 3, 2, 1]
the second child should have more than 2 candies). - For this example
ratings[1, 100, 3, 2, 1]
, we give the first child one candy. Start looping from the second child, it has more rating than the first child, then we give the second child one more candy than the first child, so second child has 2 candies. For the third child, the rating is 3, which is less than the second child, so we use a variablecnt
to count how many children starts less and less rating. When we get till the last less rating child, we can think backward, from the above example,cnt
is 3. The last less rating child is the fifth child, which gets 1 candy, this child’s previous child which is the fourth child gets 2 candies, this child’s previous child which is the third child gets 3 candies. In other word, its1+2+...+cnt
when we encounters less and less rating children. Also, we need to check start from the first less and less rating child, this child’s previous child should add candy or not. From the example, we know the third child should have 3 candies, now the second child only has 2 candies. So the second child should have 4 candies. In other word, the first getting less and less child’s previous child should add the number of(cnt+1-pre)
candy.pre
is the number of candy that the previous child has. In this example, it’spre = 2
. So we add3+1-2=2
more candy to the second child.
Solution 2
1 |
|