当前位置:网站首页>Leetcode 0135. distribute candy
Leetcode 0135. distribute candy
2022-07-25 23:35:00 【Tisfy】
【LetMeFly】135. Distribute candy
Force button topic link :https://leetcode.cn/problems/candy/
n Two kids in a row . Give you an array of integers ratings Indicates the score of each child .
You need to follow these requirements , Distribute candy to these children :
- Each child is assigned at least
1A candy . - Children with higher scores from two adjacent children will get more candy .
Please distribute candy to each child , Calculate and return what needs to be prepared Minimum number of sweets .
Example 1:
Input :ratings = [1,0,2] Output :5 explain : You can give the first one separately 、 the second 、 The third child distributed 2、1、2 Candy .
Example 2:
Input :ratings = [1,2,2]
Output :4
explain : You can give the first one separately 、 the second 、 The third child distributed 1、2、1 Candy .
The third child only got 1 Candy , This satisfies two conditions in the problem surface .
Tips :
n == ratings.length1 <= n <= 2 * 1040 <= ratings[i] <= 2 * 104
Method 1 : Find the smallest
The idea is simple : Find all of them first “ Minima ”( Previous rating And the latter rating Are greater than this rating)
Then for each minimum , from 1 1 1 Start distributing candy , And continue to extend to both sides , Until it no longer increases .
During extension , The amount of candy distributed is accumulated .

after , Go through it again “ Candy allocation array ”, If you encounter adjacent The unqualified situation , Correct the allocation of non call and condition :

- Time complexity O ( n ) O(n) O(n), among n n n It's the number of children
- Spatial complexity O ( n ) O(n) O(n)
AC Code
C++
class Solution {
public:
int candy(vector<int>& ratings) {
vector<int> mins;
vector<int> candies(ratings.size());
// look for “ Minima ”
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);
}
// Start from the minimum point and expand to both sides
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;
}
}
}
// Update the adjacent conditions that do not meet
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;
}
}
// Sum by accumulation
int ans = 0;
for (int& t : candies)
ans += t;
return ans;
}
};
It's not easy to make pictures , If you like it, just like it before you leave
Synchronous posting on CSDN, Originality is not easy. , Reprint please attach Link to the original text Oh ~
Tisfy:https://letmefly.blog.csdn.net/article/details/125977968
边栏推荐
- WordPress function encyclopedia, you can do the theme after learning it
- [QNX hypervisor 2.2 user manual]9.7 generate
- 【MUDUO】EventLoopThreadPool
- ratio学习之ratio_add,ratio_subtract,ratio_multiply,ratio_divide的使用
- R语言安装教程 | 图文介绍超详细
- Inheritance (the child constructor inherits the attributes in the parent constructor)
- 【代码案例】博客页面设计(附完整源码)
- Canada EE channel
- Npm+ module loading mechanism
- Deep and shallow copies
猜你喜欢

WebMvcConfigurationSupport

利用用户脚本优化 Yandere/Konachan 站点浏览体验
![[code case] blog page design (with complete source code)](/img/9e/0e7cab956515b9cc75a7567eb477d2.png)
[code case] blog page design (with complete source code)

Cuteone: a onedrive multi network disk mounting program / with member / synchronization and other functions

Grain Academy p98 trample pit e.globalexceptionhandler: null

Swap, move, forward, exchange of utility component learning

【JUC】并发需要了解的关键字volatile

Solution of phpstudy service environment 80 port occupied by process system under Windows

Classes and objects (3)

电商RPA,大促轻松上阵的法宝
随机推荐
Why are there many snapshot tables in the BI system?
Swap, move, forward, exchange of utility component learning
Taobao Search case
Promise asynchronous callback function
热部署和热加载有什么区别?
@Import
XxE & XML external entity injection utilization and bypass
PyTorch的数据输入格式要求及转换
[QNX hypervisor 2.2 user manual]9.8 load
Very simple vsplayaudio online music player plug-in
TS union type
What is the difference between hot deployment and hot loading?
152. 乘积最大子数组-动态规划
Data broker understanding
Moment.js
Anti shake and throttling
MVVM model
numeric学习之iota,accumulate
The VM session was closed before any attempt to power it on
1913. 两个数对之间的最大乘积差-无需排序法