Leetcode of the candy points candy problem.

Source: Internet
Author: User

Topic:

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?

Understanding the topic is an egg-sore thing.
The main idea: a few children stand in a row, each child has a grade value, now give the child sugar, hair when the time to abide by 2 rules:
(1) At least one sugar per child
(2) Among the two children in the neighborhood, the children of the same rank must have more sugar than the children of the grade, and the minimum amount of the number of the hair sugar should be obtained. (The same level can have more or less)


According to the meaning of the topic, we can know that in order to ensure that the left and right levels have a difference, the points of the candy is different. All we can do is to look at the array from the left, then the left side is bigger than the right, you can get more candy, then from the right side, the right side is bigger than the left, you can get more candy.

The specific algorithm is as follows:

Algorithm 2: Initialize all children the sugar number is 1, the previous scan, if the first child level is higher than i-1, then I sugar number is equal to the number of sugars in i-1 +1; from the back-to-front scan, if the first child of the sub-level is higher than i+1 children, but the number of sugars is small or equal, I The number of sugars in +1 is +1.

The algorithm has a time complexity of O (N)

The specific code implementation is as follows:

C + + Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
25
26
27
28
29
30
31
32
33
34
Class Solution
{
Public
int Candy (vector< int> &ratings)
{
int len = Ratings.size ();
vector< int> Candynum (len, 1);
for (int i = 1; i < Len; i++)
{
if (Ratings[i-1] < ratings[i])
{
Candynum[i] = candynum[i-1] + 1;
}
}

for (int i = len-2; I >= 0; i--)
{
if (Ratings[i] > ratings[i + 1] && Candynum[i] <= candynum[i + 1])
{
Candynum[i] = candynum[i + 1] + 1;
}
}

int ret = 0;
for (int i = 0; i < len; i++)
{
RET + = Candynum[i];
}

return ret;

}
};





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.