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?
A very easy implementation
Space Complexity: O(n) Time Complexity : O(n)
Step 1: Start traversing the array from left to right and assign the value 1 to each index in dp array.
Step 2: Start traversing the array from left to right and if the person scored more marks then the person sitting to his left then dp of that person will be dp[i-1]+1;
Step 3: Start traversing the array from right to left and if the person scored more marks then the person sitting to his right then dp of that person will be max(dp[i],dp[i+1]+1);
Step 4 : Add all the values in dp array.
Let's say We have
1, 5, 2, 10, 10 3, 8, 9 , 1 , 1, 2 as ratings of 11 students S1 to S11
Let us say we make a graph rating vs student where rating is plotted on y axis, then
Local minimas will always get one candy, so student S1 , S3, S6, S9 and S10 will be local minimas and will get one candy. We can argue by saying there is a minimum solution (So) which deviate from what we say, then we can create one more solution (So1) where all the students get same candy and local minima which deviates gets one candy, then So1 will be minimum, so therefore there can be no minimum solution where local minimas get candies more than 1.
Once you have got values of local minimas, you can transverse left and right of the minima to calculate candies of other students.
For local maxima , it will be greater of value for its adjacent nodes +1. Working code is below, time complexity is O(n) and space complexity is O(n)
You can test with test cases at HackerEarth Problem : https://www.hackerrank.com/challenges/candies
Leetcode : https://leetcode.com/problems/candy/
You can find more detailed explanation of why this works @ http://stackandqueue.com/?p=108
I don't think this is a very difficult problem. If you think of it carefully, you will get you own answer.
I used two queues to store increasing and decreasing sequences of unassigned indices and enumerate all possible situations of the adjacent ratings and assign the candies when rating hit a plateau or bottom(i.e. when current rating is local minimum or same as previous).
Here is my solution:
It passes the testcases and 3/10 cases correct but I got segmentation fault for the rest.
I am wondering whether anyone can point out what is wrong with my code.
Thank you very much!
Simple working code with an array as input, in Java language. Time Complexity: O(n).
You can do this in two passes. Start with everyone having one candy.
First loop i from 1 to n-1 (zero based), if rating[i] > rating[i-1] then candies[i] = candies[i-1]+1
Then loop i from n-2 to 0; if rating[i] > rating[i+1] then candies[i] = max(candies[i], candies[i+1]+1)
It's pretty easy to show this gives you a valid distribution of candies, since the second loop can't break anything fixed by the first, and all possible rating discrepancies must be caught by one of the two loops. It's not as obvious that this will use the minimum number of candies, but if you examine each assignment closely you can show that the conditions prove a lower bound on the number of candies required (by a particular individual) at each step.