135. Candy

Source: Internet
Author: User

First, the topic

1, examining

  

2. Analysis

Each child corresponds to a grade, each child at least one sugar, next to the higher level of the child than next to the lower level of the candy more, beg at least to divide a few sweets.

Second, the answer

1, Ideas:

Method One,

Consider the number of sweets each child has to be affected by the child's rank on both sides of the left and right. So

①, find out all the children of the lowest level of the child 1, a sugar, and his left if there is a child 2 and the level is higher than the child 1, the child 2 candy for children 1 Sweets + 1, the final child 2 points of candy to be greater than equal to this number.

Similarly, the child 1 to the right of the companion child 3 if the level is greater than the child 1, also first assigned (Child 1) +1, the child 3 the final distribution of candy to be greater than the number of times.

②, the child 1 left all children follow the ① continue to recursion, the child 1 right child according to ① recursion.

③, and finally add all the sweets assigned to the child.

 Public intCandyint[] ratings) {                intLen =ratings.length; int[] Cost =New int[Len]; Candyhelper (ratings, cost,0, Len-1); intresult = 0;  for(inti = 0; i < Len; i++) Result+=Cost[i]; returnresult; }        Private voidCandyhelper (int[] Ratings,int[] Cost,intStartintend) {                if(Start >end)return; intMinindex = start;//rating the smallest subscript         for(intj = start + 1; J <= End; J + +) {            if(Ratings[minindex] >Ratings[j]) Minindex=J; } Cost[minindex]= Math.max (Cost[minindex], 1); if(minIndex-1 >= start && ratings[minindex-1]! = Ratings[minindex])//not equal to the leftCost[minindex-1] = Math.max (Cost[minindex-1], Cost[minindex] + 1); if(Minindex + 1 <= End && Ratings[minindex + 1]! = Ratings[minindex])//not equal to rightCost[minindex + 1] = Math.max (Cost[minindex + 1], Cost[minindex] + 1); Candyhelper (ratings, cost, start, Minindex+ W); Candyhelper (ratings, cost, Minindex+ 1, end); }

Method Two,

①, create a new cost array, the initial value is 1;

②, from the previous to traverse the ratings, if the latter element of the rating is greater than the previous one, then the cost of the last element of the previous value + 1

③, from the back forward to traverse the ratings, if the previous element's rating is greater than the latter, then the cost of the previous element is the maximum value of the current cost and the latter value + 1.

     Public intCandy2 (int[] ratings) {                intLen =ratings.length; if(Len < 2)            returnLen; int[] Cost =New int[Len]; Arrays.fill (Cost,1); //sure right higher rated child gets 1 more candy than left lower rated child         for(inti = 1; i < Len; i++) {            if(Ratings[i] > Ratings[i-1]) Cost[i]= Cost[i-1] + 1; }        //Scan from sure left higher rated child gets 1 more candy than right lower rated child         for(inti = len-1; i > 0; i--) {            if(Ratings[i-1] >ratings[i]) cost[i-1] = Math.max (Cost[i-1], cost[i] + 1); }                intresult = 0;  for(inti = 0; i < Len; i++) Result+=Cost[i]; returnresult; }

135. Candy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.