Alice is a teacher of kindergarten. She wants to give some candies to the children in her class. All the children sit in a line and each of them has a rating score according to his or her usual performance. Alice wants to give at least 1 candy for each children. Because children are somehow jealousy. Alice must give her candies according to their ratings subjects to for any adjacent 2 children if one's rating is higher than the other he/she must get more candies than the other. Alice wants to save money so she wants to give as few as candies in total.
Input
The first line of the input is an integer N
, the number of children in Alice's class. Each of the following N
lines contains an integer indicates the rating of each child.
Output
On the only line of the output print an integer describing the minimum number of candies Alice must give.
Sample Input
3
1
2
2
Sample Output
4
Explanation
The number of candies Alice must give are 1,2 and 1.
Constraints:
N
and the rating of each children is no larger than 10^5.
Can anyone please help me?
1.Initialize
Ratings[]
with the given ratings.2.We'll first give 1
Candy
to each of the children. So initialize all members inVal[]
with 1.3.Now traverse through and check with the 2 adjacent children (
i + 1
andi - 1
) whether the condition is held.4.Keep doing this until we get an entire traversal where the condition is never broken. We're done then!
Think of these possible rating configurations: 1 2 3 4 5 or any increasing sequence and 5 4 3 2 1 or any decreasing sequence
what can be done in the first case? 1 2 3 4 5 is the candies that are allocated in the second case the candies are 5 4 3 2 1
The solution can be obtained by scanning the array from left to right and identifying intervals of increase and scanning from right to left and again identifying intervals of increase & allocating minimal amounts of candies in this process. for exact details, please look at the code:
Perhaps the simplest solution:
}
To summarize:
Actually this question can be solved by passing two times over the array. And the solution will be O(N) time. I solved this problem by using geeksforgeeks Trapping Rain Water (http://www.geeksforgeeks.org/trapping-rain-water/ ) problem. Same principles can be applied to this questions.
First of all we have two rules. - Each student gets at least 1 candy. - Each student who has a better rating than his/her neighbor (the next or the previous student) he will get at least one more candy then them.
Just as I've said we need two passing over the array. The first one will be from left to right; - At first we will assign one candy to the first one. - Then loop over the array by checking if current one has bigger rating then give him one more than the previous student, otherwise give him 1 candy.
The second pass will be from right to left. - This time we start by assigning one candy to the last one. - Then loop over the array from right to left and do the similar things just in the first loop.
After this two loops, we will loop over right and left by getting the maximum of the array and add this value to the total candies.
Below you can find a Java solution for this problem.