当前位置:网站首页>leetcode135. Distribute candy
leetcode135. Distribute candy
2022-07-07 08:38:00 【No woman knocks the code in the heart, natural God!!】
One : The analects of Confucius
Two : subject
3、 ... and : Upper code
class Solution {
public:
int candy(vector<int>& ratings) {
/** Ideas :1. Here we traverse twice ( From left to right From right to left ) 2. When we traverse from left to right If the score on the right is higher than that on the left Then count the candies of the children on the right Add one to the candy number of the child on the left 3. When we traverse from right to left , If the score on the left is higher than that on the right And the number of candies is less than that on the right Then the number of sweets for the child on the left = The number of sweets of the child on the right +1 */
map<int,int>m;
map<int,int>:: iterator mt;
int sum = 0;
for(int i = 0; i < ratings.size(); i++) {
m[i] = 1;
}
// From left to right
for(int i = 0; i < ratings.size()-1; i++) {
if(ratings[i+1] > ratings[i]){
// The number of candies of the children on the right is greater than the number of candies on the left
m[i+1] = m[i]+1;
}
}
// From right to left
for(int i = ratings.size()-1; i > 0; i--) {
if(ratings[i-1] > ratings[i] && m[i-1] <= m[i]) {
// m[i-1] <= m[i] Make sure to add one side of the candy
m[i-1] = m[i] + 1; // The number of sweets is less than or equal to that on the right
}
}
for(mt = m.begin(); mt != m.end(); mt++) {
sum += mt->second;
}
// int sum = accumulate(mt->second().begin(),mt->second().end(),0);
return sum;
}
};
边栏推荐
- Data type - floating point (C language)
- idea里使用module项目的一个bug
- 2-3查找樹
- 2-3查找树
- Upload an e-office V9 arbitrary file [vulnerability recurrence practice]
- PVTV2--Pyramid Vision TransformerV2学习笔记
- GOLand idea intellij 无法输入汉字
- 联想混合云Lenovo xCloud:4大产品线+IT服务门户
- Learn how to compile basic components of rainbow from the source code
- uniapp 微信小程序监测网络
猜你喜欢
数据分片介绍
Give full play to the wide practicality of maker education space
Practice of implementing cloud native Devops based on rainbow library app
調用華為遊戲多媒體服務的創建引擎接口返回錯誤碼1002,錯誤信息:the params is error
Virtual address space
Using nocalhost to develop microservice application on rainbow
如何在快应用中实现滑动操作组件
Componentspace2022, assertions, protocols, bindings, and configuration files
National standard gb28181 protocol video platform easygbs adds streaming timeout configuration
About using CDN based on Kangle and EP panel
随机推荐
Opencv learning note 4 - expansion / corrosion / open operation / close operation
[南京大学]-[软件分析]课程学习笔记(一)-introduction
归并排序和非比较排序
Fluentd is easy to use. Combined with the rainbow plug-in market, log collection is faster
Compilation and linking of programs
Rainbow 5.7.1 supports docking with multiple public clouds and clusters for abnormal alarms
Splunk query CSV lookup table data dynamic query
mysql分区讲解及操作语句
Snyk dependency security vulnerability scanning tool
数据库存储---表分区
One click deployment of highly available emqx clusters in rainbow
IP地址的类别
MySQL introduction - crud Foundation (establishment of the prototype of the idea of adding, deleting, changing and searching)
Grpc, oauth2, OpenSSL, two-way authentication, one-way authentication and other column directories
登山小分队(dfs)
Golang compilation constraint / conditional compilation (/ / +build < tags>)
Data type - floating point (C language)
In go language, function is a type
[Yu Yue education] C language programming reference of Zhongbei College of Nanjing Normal University
数据分析方法论与前人经验总结2【笔记干货】