[Leetcode] [JavaScript] Candy

Source: Internet
Author: User

Candy 

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?

https://leetcode.com/problems/candy/

Greedy, figured out that two rounds of traversal were finished.

First round first small basin with first give 1, from left to right sweep, if find current rating is smaller than left rating, give sugar more than left 1.

The second round from the bottom of the second small basin with the beginning, from right to left sweep, if found that the current rating is less than the right rating, and less sugar, give more than the left 1 sugar.

1 /**2 * @param {number[]} ratings3 * @return {number}4  */5 varCandy =function(ratings) {6     varI, result; Rates = [];7Rates[0] = 1;8      for(i = 1; i < ratings.length; i++){9         if(Ratings[i] > Ratings[i-1]){TenRates[i] = rates[i-1] + 1; One}Else{ ARates[i] = 1; -         } -     } theresult = Rates[ratings.length-1]; -      for(i = ratings.length-2; I >= 0; i--){ -         if(Ratings[i] > ratings[i + 1] && rates[i] <= rates[i + 1]){ -Rates[i] = rates[i + 1] + 1; +         } -Result + =Rates[i]; +     } A     returnresult; at};

The first thought of a solution, a bit laborious.

The first round of traversal, if this small bowl and the rating is smaller than the left and right, he must be 1, the index into the queue.

Then it starts to recursion, a bit like BFS.

In this way, to ensure that the large rating must be done, the first treatment is the smaller rating.

Recursion comes in two different situations:

1. The current ratings is less than the left and greater than the right or current ratings is less than the right and greater than left.

Because the big must be done after, so you can determine the current point of sugar, equal to Max (left sugar, right sugar) + 1, note if there is no sugar, the default value is 0, the larger side is 0.

2. If both sides have been given sugar, and the current ratings is greater than the left and greater than the right, the current node sugar number is also equal to Max (left sugar, right sugar) + 1.

After this round of sugar, the left-hand side and the current no-sugar nodes are crammed into the queue.

Although more complex, the time complexity is still O (n).

But 12000 of the stack of data stacks overflowed after the turn-up.

Alas, the local test 10 a few milliseconds duly, Leetcode's JS small stack unexpectedly exploded, is under the loss.

1 /**2 * @param {number[]} ratings3 * @return {number}4  */5 varCandymle =function(ratings) {6     varQueue = [], result = 0, rates = [];7      for(vari = 0; i < ratings.length; i++){8         if(Ratings[i-1] && ratings[i-1] <Ratings[i]) {9             Continue;Ten         } One         if(Ratings[i + 1] && Ratings[i + 1] <Ratings[i]) { A             Continue; -         } - Queue.push (i); theresult++; -Rates[i] = 1; -     } - BFS (queue); +     returnresult; -  +     functionBFS (queue) { A         varLen =queue.length; at         if(queue.length!== 0){ -              while(len--){ -                 vartop =Queue.shift (); -                 if(!Rates[top]) { - Givecandy (queue, top); -                 } in                 if(!rates[top-1] && ratings[top-1] && queue.indexof (top-1) = = = 1){ -Queue.push (top-1); to                 } +                 if(!rates[top + 1] && Ratings[top + 1] && queue.indexof (top + 1) = = = 1){ -Queue.push (top + 1)); the                 }    *                 if(!rates[top] && queue.indexof (top) = = = 1){ $ Queue.push (top);Panax Notoginseng                 }      -             } the BFS (queue);  +         } A     } the     functionGivecandy (queue, i) { +         if(Ratings[i]) { -             varLeftvalue = Ratings[i-1] | | 0; $             varleftrate = Rates[i-1] | | 0; $             varRightvalue = ratings[i + 1] | | 0; -             varRightrate = rates[i + 1] | | 0; -             if(Ratings[i] > Leftvalue && ratings[i] < Rightvalue | | theRatings[i] < Leftvalue && Ratings[i] > Rightvalue | | -Ratings[i] >= leftvalue && ratings[i] >= rightvalue && (leftrate!== 0 | |!ratings[i-1])){WuyiRates[i] = Math.max (leftrate, rightrate) + 1; theResult + =Rates[i]; -             } Wu         } -     } About};

[Leetcode] [JavaScript] 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.