【LeetCode 135】Candy

本文探讨了如何在满足特定条件下,为一排孩子分配最少数量的糖果。通过从左至右及从右至左两次遍历,确保了每个孩子的糖果数量不仅满足基本需求,还符合其与邻居之间的相对评级。

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

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.
What is the minimum candies you must give?

题意:给排成一排的小孩发糖,小孩身高有高有矮。对于任何一对相邻的小孩,两者相对较高的那个
必须得到比矮个更多的糖。此外必须保证每个人至少得到一块糖。
求解所需要准备的糖果总数。

Example 1:
Input: [1,0,2]
Output: 5
Explanation: You can allocate to the first, second and third child with 2, 1, 2 candies respectively.

Example 2:
Input: [1,2,2]
Output: 4
Explanation: You can allocate to the first, second and third child with 1, 2, 1 candies respectively.
             The third child gets 1 candy because it satisfies the above two conditions.

思路:对于任何一个小孩,他得到的糖要比左右比他矮的小孩的糖更多。这个问题其实与2D蓄水池问题类似,都是和左右比较。所以应该想到从左到右、从右到左各遍历一遍的方法。此外因为本题仅仅是和相邻元素对比,所以单调栈用处不大。

发糖的难点其实是当两相邻小孩等高时的情况,这时两人分配的糖果是存在所有可能性的。

先从左到右遍历,根据rating的大小尽可能少的发糖,即
从左到右的发糖只需要满足两点:
        每个小孩至少一块糖(所有小孩初始化1块糖);
        左边小孩较矮的话,右边小孩的糖数比他多一个。 

任何更少的糖果都无法满足这两点。
再从右到左进行修正,这时候只要满足一点:
        右边小孩较矮的话,左边小孩的糖数比他多一个。

任何更少的糖果也都无法满足这一点。
两次遍历的结合就是题设条件,而且是最低满足。

int candy(vector<int>& ratings) {
	int len = ratings.size();
	vector<int> candy(len, 1);
	//left-->right
	for (int i = 1; i<len; i++) {
		if (ratings[i]>ratings[i - 1])
			candy[i] = candy[i - 1] + 1;
	}
	//right-->left and sum
	int sum = candy[len - 1];
	for (int i = len - 2; i >= 0; i--) {
		if (ratings[i]>ratings[i + 1] && candy[i] <= candy[i + 1])
			candy[i] = candy[i + 1] + 1;
		sum += candy[i];
	}
	return sum;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值