*candy——leetcode


/* 
*/
#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值