当前位置:网站首页>LeetCode 0135. 分发糖果
LeetCode 0135. 分发糖果
2022-07-25 23:31:00 【Tisfy】
【LetMeFly】135.分发糖果
力扣题目链接:https://leetcode.cn/problems/candy/
n 个孩子站成一排。给你一个整数数组 ratings 表示每个孩子的评分。
你需要按照以下要求,给这些孩子分发糖果:
- 每个孩子至少分配到
1个糖果。 - 相邻两个孩子评分更高的孩子会获得更多的糖果。
请你给每个孩子分发糖果,计算并返回需要准备的 最少糖果数目 。
示例 1:
输入:ratings = [1,0,2] 输出:5 解释:你可以分别给第一个、第二个、第三个孩子分发 2、1、2 颗糖果。
示例 2:
输入:ratings = [1,2,2]
输出:4
解释:你可以分别给第一个、第二个、第三个孩子分发 1、2、1 颗糖果。
第三个孩子只得到 1 颗糖果,这满足题面中的两个条件。
提示:
n == ratings.length1 <= n <= 2 * 1040 <= ratings[i] <= 2 * 104
方法一:找到最小
思路很简单:先找到所有的“极小点”(前一个rating和后一个rating都大于这个rating)
然后对于每一个极小点,从 1 1 1开始分配糖果,并不断向两边延伸,直到不再递增为止。
延伸过程中,分配糖果的数量累加。

之后,再次遍历一遍“糖果分配数组”,如果遇到相邻的不符合条件的情况,就修正不呼和条件的分配:

- 时间复杂度 O ( n ) O(n) O(n),其中 n n n是小朋友的数量
- 空间复杂度 O ( n ) O(n) O(n)
AC代码
C++
class Solution {
public:
int candy(vector<int>& ratings) {
vector<int> mins;
vector<int> candies(ratings.size());
// 找“极小点”
for (int i = 0; i < ratings.size(); i++) {
if ((i - 1 >= 0 && ratings[i - 1] < ratings[i]) || (i + 1 < ratings.size() && ratings[i + 1] < ratings[i])) {
continue;
}
mins.push_back(i);
}
// 从极小点开始向两边拓展
for (int thisMin : mins) {
int thisCandy = 1;
int i = thisMin;
while (true) {
candies[i] = thisCandy;
thisCandy++;
if (i - 1 >= 0 && ratings[i - 1] > ratings[i]) {
i--;
}
else {
break;
}
}
i = thisMin;
thisCandy = 1;
while (true) {
candies[i] = thisCandy;
thisCandy++;
if (i + 1 < ratings.size() && ratings[i + 1] > ratings[i]) {
i++;
}
else {
break;
}
}
}
// 更新相邻的不满足条件的情况
for (int i = 1; i < candies.size(); i++) {
if (ratings[i - 1] > ratings[i] && candies[i - 1] <= candies[i]) {
candies[i - 1] = candies[i] + 1;
}
if (ratings[i] > ratings[i - 1] && candies[i] <= candies[i - 1]) {
candies[i] = candies[i - 1] + 1;
}
}
// 累加求和
int ans = 0;
for (int& t : candies)
ans += t;
return ans;
}
};
图片制作不易,喜欢了就点个赞再走吧
同步发文于CSDN,原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/125977968
边栏推荐
- ratio学习之ratio_add,ratio_subtract,ratio_multiply,ratio_divide的使用
- WordPress controls the minimum and maximum number of words of article comments
- 策略模式_
- Family relationship calculator wechat applet source code
- 2022 Niuke multi School Game 2
- 762. Prime number calculation setting in binary representation
- 【MUDUO】EventLoopThreadPool
- Source code of wechat applet for discerning flowers and plants / source code of wechat applet for discerning plants
- S4/HANA ME21N创建PO 输出控制消息按钮丢失解决方法(切换EDI 输出模式BRF+至NAST模式)
- Kotlin 常用知识点汇总
猜你喜欢

Family relationship calculator wechat applet source code

WordPress removes the website publishing time

E-commerce RPA, a magic weapon to promote easy entry

Network Security Learning notes-1 file upload

ratio学习之ratio_add,ratio_subtract,ratio_multiply,ratio_divide的使用

Discuz atmosphere game style template / imitation lol hero League game DZ game template GBK

Apple CMS V10 template /mxone Pro adaptive film and television website template

动态内存管理

POI special effects Market Research

initializer_list工具库学习
随机推荐
谷粒学苑P98踩坑 e.GlobalExceptionHandler : null
Anti shake and throttling
utility实用组件学习之swap,move,forward,exchange
推荐系统——An Embedding Learning Framework for Numerical Features in CTR Prediction
[QNX hypervisor 2.2 user manual]9.6 GDB
Grain Academy p98 trample pit e.globalexceptionhandler: null
About priority queues
Serialize common default values and column parameters
Moment.js
Call Gaode map -- address is converted into longitude and latitude
Tencent map API request source is not authorized, this request source domain name
Computed and watch listening properties
Thinkphp6 temporarily close the layout
1913. Maximum product difference between two number pairs - no sorting required
Strategy mode_
WordPress removes the website publishing time
numeric学习之iota,accumulate
What is the difference between hot deployment and hot loading?
[wechat applet] page navigation
Node Foundation