当前位置:网站首页>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;
}
};
边栏推荐
- [kuangbin] topic 15 digit DP
- mysql分区讲解及操作语句
- The field value in Splunk subquery fuzzy matching CSV is*
- SSM 整合
- grpc、oauth2、openssl、双向认证、单向认证等专栏文章目录
- POJ - 3616 Milking Time(DP+LIS)
- Mock.js用法详解
- Basic data types and string types are converted to each other
- 23 Chengdu instrument customization undertaking_ Discussion on automatic wiring method of PCB in Protel DXP
- Learn how to compile basic components of rainbow from the source code
猜你喜欢
Iptables' state module (FTP service exercise)
Installation and configuration of PLSQL
[paper reading] icml2020: can autonomous vehicles identify, recover from, and adapt to distribution shifts?
Laravel8 uses passport login and JWT (generate token)
调用华为游戏多媒体服务的创建引擎接口返回错误码1002,错误信息:the params is error
SSM integration
Deit learning notes
MySQL introduction - crud Foundation (establishment of the prototype of the idea of adding, deleting, changing and searching)
Tuowei information uses the cloud native landing practice of rainbow
Opencv learning note 3 - image smoothing / denoising
随机推荐
PVTV2--Pyramid Vision TransformerV2学习笔记
Give full play to the wide practicality of maker education space
Through the "last mile" of legal services for the masses, fangzheng Puhua labor and personnel law self-service consulting service platform has been frequently "praised"
Novice entry SCM must understand those things
POJ - 3784 Running Median(对顶堆)
Qt Charts使用(重写QChartView,实现一些自定义功能)
Using helm to install rainbow in various kubernetes
Open3d ISS key points
Pvtv2--pyramid vision transformer V2 learning notes
归并排序和非比较排序
数据分析方法论与前人经验总结2【笔记干货】
Grpc, oauth2, OpenSSL, two-way authentication, one-way authentication and other column directories
GFS distributed file system
【踩坑】nacos注册一直连接localhost:8848,no available server
A method for quickly viewing pod logs under frequent tests (grep awk xargs kuberctl)
One click deployment of highly available emqx clusters in rainbow
PLSQL的安装和配置
更改当前文件夹及文件夹下文件日期shell脚本
JS的操作
AVL balanced binary search tree