LeetCode 135. Candy (O(n)时间 O(n)和O(1)空间的两种实现)

贪心算法,从局部最优推广成全局最优。

这里介绍O(n)时间 O(n)和O(1)空间的两种实现方法。

O(n)时间 O(n)空间实现,参考了cnblog, 1957的解法

创建candy数组,初始化为1. 用pre_child_candy记录前一个孩子拿到的糖果数

1. 从左往右遍历

1) 如果ratings[i] > ratings[i-1], 那么candy[i] = ++ pre_child_candy, 亦即这个孩子比前一个孩子多拿一个糖果

2) 反之,candy[i] = pre_child_candy = 1. (注意到我们已经将candy全部初始化为1了)

2. 从右往左遍历,pre_child_candy初始化为1, 做类似的操作

1) 如果ratings[i-1] > ratings[i], 那么candy[i] = max(++ pre_child_candy, candy[i]), 注意到这里采用max函数是因为此时的candy同时应该满足1中从左往右遍历的约束。

2) 反之,candy[i] = pre_child_candy = 1.

这样我们最低限度地实现了题目的约束,每个较高分的孩子所得的糖果都比其相邻的孩子所得的糖果多,从局部最优推广到了全局最优。

代码:

class Solution
{
public:
    int candy(vector<int> &ratings)
    {
    	vector<int> candy(ratings.size(), 1);    

    	for (int pre_child_candy=1, i=1; i < ratings.size(); ++ i)
    	{
    		if (ratings[i] > ratings[i-1])
    		{
    			candy[i] = ++ pre_child_candy;
    		} else
    		{
    			pre_child_candy = 1;
    		}
    	}
    	for (int pre_child_candy=1, i=static_cast<int>(ratings.size())-2; i >= 0; -- i)
    	{
    		if (ratings[i] > ratings[i+1])
    		{
    			candy[i] = max(++ pre_child_candy, candy[i]);
    		} else
    		{
    			pre_child_candy = 1;
    		}
    	}

    	return accumulate(candy.begin(), candy.end(), 0);
    }
};

O(n)时间 O(1)空间实现,参考了cnblog, Felix的博客,但不同的是,在此对递增、递减、相等三种情况进行了分类讨论

tot: 糖果总数,初始化为1

dec_length: 递减序列的长度,初始化为0

candy_before_dec: 给出现递减序列前的那个孩子发出的糖果,初始化为1

pre_child_candy: 给前一个孩子发出的糖果,初始化为1

1. 如果仅考虑递增序列

亦即恒有 ratings[i] > ratings[i - 1]

从下标i = 1开始访问,那么只需要在每次迭代中,执行tot += ++ pre_child_candy即可。

2. 考虑含有递减子序列的ratings[] = {1, 4, 3, 2, 1}; 正确的糖果数应该为1, 4, 3, 2, 1

i = 1, ratings[1] > ratings[0],

tot += ++ pre_child_candy; //第二个孩子只分到了2个糖果,先别着急,后面会“补偿”他
pre_child_candy, candy_before_dec都指向第二个孩子,此时它们也都等于2. 也就是

i = 2, ratings[2] < ratings[1] (ratings[i] < ratings[i - 1])

我们认为这是递减序列的开始,++ dec_length; (dec_length等于1了).
tot += dec_length; // tot += 1
pre_child_candy = 1; // 更新它,防止潜在的递增序列需要这个变量

i = 3, ratings[3] <ratings[2]

++ dec_length; (dec_length == 2 now)
tot += dec_length; // tot+=2
pre_child_candy = 1; // 更新它,防止潜在的递增序列需要这个变量
// 这时候发现,dec_lenght >= pre_child_candy,
// 递减序列的长度已经大于“给出现递减序列前的那个孩子发出的糖果”,我们就补偿给第二个孩子1个糖果,++ tot

i = 4, ratings[4] < ratings[3]

++ dec_length; (dec_length == 3 now)
tot += dec_length; // tot+=3
pre_child_candy = 1; // 更新它,防止潜在的递增序列需要这个变量
// 这时候发现,dec_lenght >= pre_child_candy,
// 递减序列的长度已经大于“给出现递减序列前的那个孩子发出的糖果”,我们就补偿给第二个孩子1个糖果,++ tot

我们最后就发现,我们成功的给第二个孩子了4个糖果,而i=2,3,4的情况,则分别给tot增加了1,2,3, 这刚好是实际情况的逆序

所以,我们处理好了含有递减子序列的情况

3. 考虑相邻孩子ratings相等的情况,可以考虑ratings[] = {1, 4, 4, 3, 2, 1}

从逻辑上,这个孩子暂时只需要给1个糖果,同时将递减序列长度归零,candy_before_dec和pre_child_candy置1即可。

代码:

class Solution
{
public:
    int candy(vector<int> &ratings)
    {
    	int tot = 1, dec_length = 0, candy_before_dec = 1, pre_child_candy = 1;

    	for (int i = 1; i < ratings.size(); ++ i)
    	{
    		if (ratings[i] > ratings[i-1])
    		{
    			tot += ++ pre_child_candy;
    			dec_length = 0;
    			candy_before_dec = pre_child_candy;
    		} else if (ratings[i] == ratings[i-1])
    		{
    			++ tot;
    			dec_length = 0;
    			candy_before_dec = pre_child_candy = 1;
    		} else // <
    		{
    			tot += ((++dec_length>=candy_before_dec)? 1: 0);
    			tot += dec_length;
    			pre_child_candy = 1;
    		}
    	}

    	return tot;
    }
};
时间: 2025-01-02 03:30:11

LeetCode 135. Candy (O(n)时间 O(n)和O(1)空间的两种实现)的相关文章

LeetCode 135 Candy(贪心算法)

135. 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 mo

leetcode[135] Candy

最少糖果问题.一排小孩,每个孩子有一个优先级,每个孩子至少要发给一个糖果,优先级高的比周围的孩子的糖果要多. 需要注意的是,优先级一样的没有要求说一样多糖果! 先初始化,每人一糖. 为了保证优先级大的比相邻的且优先级小的要糖果多.所以我们分两次处理,一次处理比左边的多,一次处理兼顾左边的多的情况下比右边的多.(如果优先级满足多的前提) 那么第一次从左到右,if (ratings[i] > ratings[i-1]) 那么cd[i] = cd[i-1]+1;    cd指糖果数组 从右边到左边需要

leetcode 135. Candy ----- 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 candies

Java for LeetCode 135 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

leetcode -day9 Candy &amp; Gas Station &amp; Binary Tree Maximum Path Sum

1.  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 m

[LeetCode][JavaScript]Candy

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 ca

偶然看到的面试算法题_最短时间找出十包粉末中的两蓝粉末。

题目:有4个杯子,10包粉末,其中有2包溶于水变蓝,其余无色,粉末溶于水2min才能显现颜色.求找出两包蓝色粉末的最短时间.假设水和粉末用不完. 解:以下给出四种解法,标记10包粉末为(1,2 ... ) 杯子为[1,2,3,4]首先我想会不会是有某种算法,dp 二分..@[email protected]..没有,懵懵的. 法一:这是我最初想到的比较傻的方法 第一趟:[12,34,56,78] 每个杯子分别放两包加水融化,剩下两包不管.可能的情况: (1)0个杯子变色,说明剩下两包就是蓝粉末