leetcode candy

本文探讨了一段复杂的代码实现过程,通过逐步分析和优化,最终实现了对问题的高效解决。介绍了如何通过结构化思考和算法优化来简化代码逻辑,提高程序运行效率。文章深入剖析了代码中涉及的数据结构和算法应用,展示了从复杂到简洁的转变过程,为开发者提供了实用的代码优化技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

果然还是自己的代码有问题。

来回改了好几遍。。


struct CandyNode{
public:
	int val;
	int rate;
	CandyNode *pre, *next;
	CandyNode(int r) :val(1),rate(r), pre(NULL), next(NULL){}
};


int candy(vector<int> &ratings) {
	int res = 0, n = ratings.size();
	if (n == 0)return 0;
	int i = 0;
	CandyNode *head = new CandyNode(ratings[0]);
	CandyNode *p = head, *backp;
	for (i = 1; i < ratings.size(); i++){
		CandyNode *tmp = new CandyNode(ratings[i]);
		p->next = tmp;
		tmp->pre = p;
		/*if (p->rate == tmp->rate)
		{
			tmp->val = 1;
		}*/
		if (p->rate < tmp->rate){
			if (p->pre && p->pre->rate >= p->rate)p->val = 1;
			backp = p;
			while (backp->pre && backp->pre->rate >= backp->rate)
			{
				if (backp->pre->rate == backp->rate && backp->pre->val < backp->val){
					if ((!backp->pre->pre) || (backp->pre->pre && backp->pre->pre->rate < backp->pre->rate))break;
					if ((!backp->pre->pre) || (backp->pre->pre && backp->pre->pre->rate >= backp->pre->rate)){
						backp->pre->val = 1;
					}
					else{
						backp->pre->val = backp->val;
					}
				}
				else if (backp->pre->rate > backp->rate && backp->pre->val <= backp->val){
					backp->pre->val = backp->val + 1;
				}
				backp = backp->pre;
			}
			tmp->val = p->val + 1;
		}

		//last node
		if (i == ratings.size() - 1){
			backp = tmp;
			while (backp->pre && backp->pre->rate >= backp->rate)
			{
				if (backp->pre->rate == backp->rate && backp->pre->val < backp->val){
					if ((!backp->pre->pre) || (backp->pre->pre && backp->pre->pre->rate < backp->pre->rate))break;
					backp->pre->val = 1;
				}
				else if (backp->pre->rate > backp->rate && backp->pre->val <= backp->val){
					backp->pre->val = backp->val + 1;
				}
				backp = backp->pre;
			}
			if (backp == head){
				if (backp->rate > backp->next->rate)backp->val = backp->next->val + 1;
				else if (backp->rate == backp->next->rate)backp->val = backp->next->val;
			}
		}
		p = tmp;
	}

	while (head){
		res += head->val;
		head = head->next;
	}
	return res;
}



别人的代码还是那么简单。。。

class Solution {
public:
    int candy(vector<int> &ratings) {
        int n = ratings.size();
        int min_candies = n;

        int range = 0;
        for (size_t i = 0; i < n - 1; ) {
            int start = i;
            while (ratings[i] < ratings[i+1] && i < n-1) ++i;
            range = i - start;
            min_candies += (range * (range + 1)) / 2;
            if (i == n-1) break;

            start = i;
            while (ratings[i] > ratings[i+1] && i < n-1) ++i;
            int k = i - start - 1;
            min_candies += (k * (k + 1)) / 2;
            if (i - start > range) min_candies += (i - start - range);
            if (ratings[i] == ratings[i+1]) ++i;
        }
        return min_candies;
    }
};


这个还没仔细看:听说是O(N),空间O(1)

class Solution {
public:
    int candy(vector<int> &ratings) {

/***  The solution is O(N) && space O(1) ***/
/***  only one pass for time && only 4 int on stack for storage ***/

        int current, peak, back, sum;

        if (ratings.empty()) return 0;

        current=1;
        sum=1;
        back=1;
        peak=INT_MAX;

        for (int i=1;i<ratings.size();i++) {
            if (ratings[i]>ratings[i-1]) {
                current++;
                sum += current;
            }

            else if (ratings[i]==ratings[i-1]) {   

            /**  Take care of equal case: equal ratings does not need equal candy. 
            thus, when ratings[i]==ratings[i-1]  simply give child i 1 candy. no need 
            to store i-1's candy as peak because i can go up infinitely without the 
            necessity to bump i-1 up **/

                peak=INT_MAX; // no need to check
                current=1;
                sum +=current;
                back=1;
            }
            else {
                if (current!=1) {// drop to 1 
                    peak=current;
                    current=1;
                    sum += current;
                    back=1;
                }

                else { // bump all previous (all the way to back's position) up by 1
                    back++;
                    sum += back;
                    if (back==peak) {   // can only be true when dropping to 1 happen when ratings[i]<
                                             // ratings[i-1],  for equal case, we set peak to INT_MAX to 
                                             //prevent it from happening here
                        sum++; peak++;
                    }
                }
            }
        }

        return sum;
    }
};





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值