Leetcode 135. Candy Candy + very classic greedy algorithm __ algorithm

Source: Internet
Author: User

There are N children standing in a line. Each child is assigned a rating value.

You are are giving candies to this children subjected to the following:

Each child must have at least one candy.
Children with a higher rating to more candies than their.
What is the minimum candies for you must give?

The typical greedy algorithm problem itself can be done with the greedy method, we use Candy[n] to express the number of sweets per child, in the traversal process, if the child i+1 rate greater than the child I rate, then the best choice is naturally: give the child i+1 candy number = give the child I candy number +1 if the child i+ 1 rate is less than equal to the rate of child I. This time is not good to do, because we do not know the best choice is to give children i+1 how much candy.

The code is as follows:

Import Java.util.Arrays;  * * Typical greedy algorithm * The problem itself can be done with the greedy method, we use candy[n] to denote the number of sweets per child, in the traversal process, * if the child i+1 rate greater than the child I rate, then the best choice is naturally: give the child i+1 candy number = give the child I candy number +1 * If the child i+1 the rate is less than equal to the child I rate the whole.
 This time is not good to do, * because we do not know the best choice is to give children i+1 how much candy. * The solution is: do not handle this situation for the time being.
 After the array traversal, we once again traversing the array from the end of the head, * this back to the greedy, we can deal with the previous skipped child.
 * The last cumulative Candy[n] is the minimum number of sweets. 
            * */public class Solution {public int candy (int[] ratings) {if (Ratings==null | | ratings.length<=0)

        return 0;
        int []num = new Int[ratings.length];

        Arrays.fill (num, 1); for (int i=1;i<ratings.length;i++) {if (ratings[i]>ratings[i-1]) num[i]=num[i-1]+
        1; for (int i=ratings.length-2;i>=0;i--) {if (ratings[i]>ratings[i+1] && Num[i] & Lt
        num[i+1]+1) num[i]=num[i+1]+1;
        int sum=0;
        for (int i=0;i<num.length;i++) sum+=num[i]; 
    return sum; }
}

Here is the practice of C + +, this problem is a typical greedy algorithm of a simple application, need to study hard

Pay attention to the comparison of DP in reverse traversal

The code is as follows:

 #include <iostream> #include <vector> #include <map> #include < unordered_map> #include <set> #include <unordered_set> #include <queue> #include <stack> # Include <string> #include <climits> #include <algorithm> #include <sstream> #include < functional> #include <bitset> #include <numeric> #include <cmath> #include <regex> using name



Space Std;
        Class Solution {Public:int candy (vector<int>& r) {int n = r.size ();
        Vector<int> DP (n, 1);
        for (int i = 1; i < n; i++) {if (R[i] > R[i-1]) dp[i] = dp[i-1] + 1; for (int i = n-2 i >= 0; i--) {if (R[i] > r[i + 1] && Dp[i] < Dp[i
        + 1]+1) Dp[i] = dp[i + 1] + 1;
    return accumulate (Dp.begin (), Dp.end (), 0); }
};

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.