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?
I don't feel too hard about the topic itself.
The landlord is thinking like this.
Use a candy array [0...ratings.length-1]
Initialize the first child to get a sugar, i.e. candy[0] = 1;
Scan this ratings array starting from 1,
If an ascending sequence is encountered, candy[i] = candy[i-1] + 1, at the same time, take a Lasthigh variable to record the index of the current ascending sequence. In other words,Lasthigh stores the indexof the peak of the last crest.
If it encounters an equal, candy[i] = 1. If this is a face2face interview, you should ask the interviewer if ratings equal children need to get the same sugar. This topic does not have this requirement, so if encountered equal, we immediately give a sugar, then update lasthigh. The first time I thought, I believed that "Lasthigh is the indexof the peak of the last crest", so I think, "in this step, only ratings[lasthigh] = = Ratings[i], update Lasthigh to I". Thus, when encountering an equal crest, the Lasthigh always points to the last high point (:> on the edge of the cliff).
If you encounter a descending sequence, it's a bit complicated here:
1) First Candy[i] = 1, because the minimum number of sugar it is;
2) Then for the candy from Lasthigh + 1 to i-1, we are going to have +1, then, in other words, Total's number of sugars increased by I-lasthigh. Notice that this is an optimization, we do not need to really go through candy[lasthigh...i], and then add 1.
3) to Lasthigh, note that since the descent sequence may be long, resulting in Candy[lasthigh + 1] = = Candy[lasthigh], then we also need to add candy[lasthigh]. Otherwise this step does not have to do (Candy[lasthigh] > candy[lasthigh + 1)
The code is as follows:
Public intCandyint[] ratings) { if(Ratings.length = = 0) { return0; } int[] Candy =New int[Ratings.length]; candy[0] = 1; intLasthigh = 0; intTotal = 1; for(inti = 1; i < ratings.length; i++) { if(Ratings[i] > Ratings[i-1]) {Candy[i]= Candy[i-1] + 1; Lasthigh=i; Total+=Candy[i]; } Else if(Ratings[i] = = Ratings[i-1]) {Candy[i]= 1 ; if(Ratings[lasthigh] = = Ratings[i]) {//walk to the edge of the crest of the cliffLasthigh =i; } Total+=Candy[i]; } Else{Candy[i]= 1; Total+ = i-Lasthigh; if(I! = Lasthigh + 1) {Candy[lasthigh+ 1]++; } if(Candy[lasthigh] = = Candy[lasthigh + 1]) {Candy[lasthigh]++; Total+ = 1; } } } //AssertValid (ratings, candy); returnTotal ; }
View Code
Then, it was sad to find that the super-large set how all WA.
Then the card for a long time (movie subtitle: "Ten years later ... “)
The landlord the last case of the array downloaded down, a look at the 9,999 elements. Can only write a file to read.
Because there is no problem with the confidence algorithm, it is certainly what corner case did not take into account, so in the algorithm introduced later wrote an assert function to confirm the legitimacy of the candy array. The main consideration is that for each element, if ratings is greater than two neighbors, then the candy array is the same. However, I suspect that in judging the equal ratings will not have any moth, it is also assert: for the same continuous rating elements, they can not get the same candy (otherwise, it is not the minimum number of candy AH). Thereupon found this sequence (the current value is I, in 3250):
I
Ratings: 9734 8116 8116 3250
Candy:2 1 1 0
Do you see the problem? Lasthigh was at the time of 9734.
If you continue to update, follow the algorithm,
Candy:3 2 2 1
However, the minimum sugar, for this sequence, should be:
Candy: 2 1 2 1
Think about, the original Lasthigh, should not be recorded crest, its significance should be: for the current I, the record can be arbitrarily grown without being affected by the other constraints of the element index.
For example, in the above sequence, the last (8116) of the equal sequential sequence should be lasthigh, because it can grow indefinitely, satisfying the lower ratings elevation of the back.
Therefore, the only change in the above code is to update the Lasthigh variable in a timely manner when encountering an equal sequence.
Public intCandyint[] ratings) { if(Ratings.length = = 0) { return0; } int[] Candy =New int[Ratings.length]; candy[0] = 1; intLasthigh = 0; intTotal = 1; for(inti = 1; i < ratings.length; i++) { if(Ratings[i] > Ratings[i-1]) {Candy[i]= Candy[i-1] + 1; Lasthigh=i; Total+=Candy[i]; } Else if(Ratings[i] = = Ratings[i-1]) {Candy[i]= 1 ; Lasthigh=i; Total+=Candy[i]; } Else{Candy[i]= 1; Total+ = i-Lasthigh; if(I! = Lasthigh + 1) {Candy[lasthigh+ 1]++; } if(Candy[lasthigh] = = Candy[lasthigh + 1]) {Candy[lasthigh]++; Total+ = 1; } } } //AssertValid (ratings, candy); returnTotal ; }
Leetcode Note Candy (tough commissioning)