首页 > 代码库 > *candy——leetcode

*candy——leetcode

三种解法:推荐第一种和最后一种,

方法一:遍历2次,时间复杂度:O(n),看见复杂度:o(n)

方法三:遍历一次,时间复杂度:o(n), 空间复杂大:o(1)

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

You are giving candies to these children subjected to the following requirements:

    Each child must have at least one candy.
    Children with a higher rating get more candies than their neighbors.
*/
#include<iostream>
#include<vector>
//#include<algorithm>
#include <windows.h> 

using namespace std;
#define STOP system("pause");
#if 1
class Solution {
public:
	int candy(vector<int> &ratings) {
		int len = ratings.size();
		int *candy = new int[len]{1};//只有candy[0]=1,others = 0;
		//vector<int> candy(len, 1);
		for (int i = 1; i < len; i++){
			if (ratings[i] > ratings[i - 1]){
				candy[i] = candy[i - 1] + 1;
			}
			else candy[i] = 1;
		}
		for (int i = len - 2; i >= 0; i--){
			if (ratings[i] > ratings[i + 1]){
				candy[i] =max(candy[i], candy[i + 1] + 1);
			}
		}
		int res{};
		for (int i = 0; i < len; i++){
			res += candy[i];
		}
		return res;
	}
};
#elif 0
class Solution {
public:
	int candy(const vector<int>& ratings) {
		vector<int> f(ratings.size());
		int sum = 0;
		for (int i = 0; i < ratings.size(); ++i)
			sum += solve(ratings, f, i);
		return sum;
	}
	int solve(const vector<int>& ratings, vector<int>& f, int i) {
		if (f[i] == 0) {
			f[i] = 1;
			if (i > 0 && ratings[i] > ratings[i - 1])
				f[i] = max(f[i], solve(ratings, f, i - 1) + 1);
			if (i < ratings.size() - 1 && ratings[i] > ratings[i + 1])
				f[i] = max(f[i], solve(ratings, f, i + 1) + 1);
		}
		return f[i];
	}
};
#elif 0
class Solution {
public:
int candy(vector<int> &ratings) {
	// Note: The Solution object is instantiated only once and is reused by each test case.
	int len = ratings.size();
	int nCandyCnt = 1;///Total candies
	int nSeqLen = 0;  /// Continuous ratings descending sequence length
	int nPreCanCnt = 1; /// Previous child's candy count
	int nMaxCntInSeq = nPreCanCnt;
	//if (ratings.begin() != ratings.end())
		//for (vector<int>::iterator i = ratings.begin() + 1; i != ratings.end(); i++)
		for (int i = 1; i < len; i++)
		{
			// if r[k]>r[k+1]>r[k+2]...>r[k+n],r[k+n]<=r[k+n+1],
			// r[i] needs n-(i-k)+(Pre's) candies(k<i<k+n)
			// But if possible, we can allocate one candy to the child,
			// and with the sequence extends, add the child's candy by one
			// until the child's candy reaches that of the prev's.
			// Then increase the pre's candy as well.

			// if r[k] < r[k+1], r[k+1] needs one more candy than r[k]
			// 
			if (ratings[i] < ratings[i - 1])
			{
				//Now we are in a sequence
				nSeqLen++;
				if (nMaxCntInSeq == nSeqLen)
				{
					//The first child in the sequence has the same candy as the prev
					//The prev should be included in the sequence.
					nSeqLen++;
				}
				nCandyCnt += nSeqLen;
				nPreCanCnt = 1;
			}
			else
			{
				if (ratings[i] > ratings[i - 1])
				{
					nPreCanCnt++;
				}
				else
				{
					nPreCanCnt = 1;
				}
				nCandyCnt += nPreCanCnt;
				nSeqLen = 0;
				nMaxCntInSeq = nPreCanCnt;
			}
		}
	
	return nCandyCnt;
}
};
#endif
void test0(){
	vector<int> a{ 0, 1, 3,1,2,3,4,5,6,5,4,3,2,1 };
	int r[10]{1, 2};
	Solution ss;
	ss.candy(a);
}	

int main(){
	LARGE_INTEGER nFreq;
	LARGE_INTEGER nBeginTime;
	LARGE_INTEGER nEndTime;
	double time;
	QueryPerformanceFrequency(&nFreq);
	QueryPerformanceCounter(&nBeginTime);
	test0();
	QueryPerformanceCounter(&nEndTime);
	time = (double)(nEndTime.QuadPart - nBeginTime.QuadPart) / (double)nFreq.QuadPart;
	cout << time << endl;
	STOP;
	return 0;
}

*candy——leetcode