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

时间: 2024-12-22 17:02:33

*candy——leetcode的相关文章

Candy leetcode java

题目: 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 cand

Candy -- leetcode

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

Candy [leetcode] O(n)时间复杂度,O(1)空间复杂度的方法

对于ratings[i+1],和ratings[i]的关系有以下几种: 1. 相等.相等时ratings[i+1]对应的糖果数为1 2.ratings[i + 1] > ratings[i].在这种情况下,要寻找以ratings[i]开始的递增序列. 3.ratings[i + 1] < ratings[i].在这种情况下,要寻找以ratings[i]开始的递减序列. 对于任意一个递增序列 [2 3 4 5 6] 对应的糖果数为 [1 2 3 4 X] 对于任意一个递减序列[6 5 4 3 2

Solution to LeetCode Problem Set

Here is my collection of solutions to leetcode problems. LeetCode - Course Schedule LeetCode - Reverse Linked List LeetCode - Isomorphic Strings LeetCode - Count Primes LeetCode - Remove Linked List Elements LeetCode - Happy Number LeetCode - Bitwise

[leetcode]Candy @ Python

原题地址:https://oj.leetcode.com/problems/candy/ 题意: 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

LeetCode || Candy

Candy Total Accepted: 12392 Total Submissions: 68386My Submissions 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

[LeetCode 题解]:Candy

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

【leetcode】Candy

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

【leetcode】Candy(python)

题目要求比其高的邻居要比本身的奖励多,那么最少也要多一个,所有我们可以找到所有的凹点,凹点如下三种情形. 找到所有的凹点后,我们就可以从凹点处开始向左右两个方向依次查找递增序列,其中每个高的都要比相邻的矮的多一个,比如1,2,5,4.我们找到凹点为1 和4,那么从1开始向左没有其他点,我们向右,依次得到2 比1高,2的糖果应该是1的基础上加1,为2, 5比2高,5的糖果是在2的基础上加1,为3.令一个凹点4, 向左,5比4高,5的糖果应该是在4的基础上加 1,为2,这时我们发现冲突了,从凹点1