Two solutions of Leetcode Candy

Source: Internet
Author: User
Tags reset
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?

The key to this problem: or examining. Feel Leetcode topic examining are difficult not to judge clearly the topic to do, often very waste of time.

Or the best way to explain the problem:

For example, I began to consider: 1 2 3 3 3 This should result in 12, but in fact the requirement is 8.

1 2 3 2 3 I thought the result was 11, but in fact it was 9.

You can read the meaning. His request is actually the "simplification" of the topic, but it has caused us to think more of the more effort.

After figuring out the problem, it's good to be done.

Here is the first solution:

#include <iostream> #include <vector> using namespace std; Class Solution {Public:int candy (vector<int> &ratings) {//Important:please reset any member data

		Lared, AS//the same solution instance would be a reused for each test case.

		if (Ratings.empty ()) return 0;

		int n = ratings.size ();
		Vector<int> candynum (n);
		You cannot use the for (auto X:candynum) x=1, so you cannot modify the value of Candynum.
		for (int i=0; i<n; i++) {candynum[i] = 1;
		} for (int i=1; i<n; i++) {if (Ratings[i-1]<ratings[i]) candynum[i] = candynum[i-1]+1; } for (int i=n-2; i>=0; i--) {if (Ratings[i+1]<ratings[i] && candynum[i]< (candynum[i+1]+1)) can
		Dynum[i] = candynum[i+1]+1;
		} int sum = 0;

		for (int i=0; i<n; i++) sum + = Candynum[i];
	return sum;


}
};
	int main () {int a[] = {1,2,3,3,3};
	Vector<int> va (A, a+5);
	Solution Solu;

	Cout<<solu.candy (VA) <<endl;
return 0; }

In fact, the vector created by the time is zero, understand this property can save the first assignment loop, and then slightly improved, as follows:

Class Solution {public
:
	int. candy (vector<int> &ratings) {
		//important:please reset any member Da Ta you declared, as
		//the same solution instance would be a reused for each test case.

		if (Ratings.empty ()) return 0;

		int n = ratings.size ();

		Using the vector attribute, initialize to 0
		vector<int> candynum (n,0);

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

		int sum = 0;
		for (int i=0; i<n; i++)
			sum + = Candynum[i] + 1;
		Using the vector attribute, the initialization element is zero, the number of candynum stored is 1 less than the actual number, so add 1

		return sum;
	}
};

The following is a loop, but nested in the middle loop, and because of this, so the efficiency is low, no accepted. But also a good idea, so paste it out to share under:

struct Twoint {
	int left;
	int right;
	Twoint (): Left (1), right (1) {}
};

Class Solution {public
:
	int. candy (vector<int> &ratings) {
		//important:please reset any member Da Ta you declared, as
		//the same solution instance would be a reused for each test case.

		if (Ratings.empty ()) return 0;

		int n = ratings.size ();

		Vector<twoint> candynum (n);

		for (int i=0, i<n; i++)
		{for
			(int k=i; k<n-1; k++)
			{
				if (ratings[k]<=ratings[k+1])
					break;
				candynum[i].right++;
			}
			for (int k=i, k>0; k--)
			{
				if (ratings[k]<=ratings[k-1]) break
					;
				candynum[i].left++;
			}

			Candynum[i].left = 
				candynum[i].left>=candynum[i].right? candynum[i].left:candynum[i].right;
		}

		int sum = 0;
		for (int i=0; i<n; i++)
			sum + = Candynum[i].left;

		return sum;
	}
};



Summarize:

Do Leetcode pay attention to examining Ah, this is made out of the experience. And do not know whether the leetcode is intentional, the topic is not verbose, the output is not much to give, the reader is not seriously read the topic is the responsibility of the reader.


Updated very concise program, the ultimate version, can not continue to clear and concise.

2014-2-18 Update
	int candy (vector<int> &ratings) 
	{
		int *a = new int[ratings.size ()];
		A[0] = 1;
		for (int i = 1; i < ratings.size (); i++)
		{
			if (Ratings[i] > Ratings[i-1]) a[i] = a[i-1]+1;
			else a[i] = 1;
		}
		int ans = a[ratings.size ()-1];
		for (int i = Ratings.size ()-2; I >= 0; i--)
		{
			if (Ratings[i] > Ratings[i+1]) a[i] = max (A[i], a[i+1]+1) ;
			Ans + = A[i];
		}
		return ans;
	}



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.