当前位置:网站首页>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;
}
};
边栏推荐
- grpc、oauth2、openssl、双向认证、单向认证等专栏文章目录
- 字符串操作
- IP地址的类别
- Teach you how to select PCB board by hand (II)
- Opencv learning note 4 - expansion / corrosion / open operation / close operation
- Compilation and linking of programs
- All about PDF crack, a complete solution to meet all your PDF needs
- 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"
- GFS分布式文件系统
- [paper reading] icml2020: can autonomous vehicles identify, recover from, and adapt to distribution shifts?
猜你喜欢

数据分片介绍

联想混合云Lenovo xCloud:4大产品线+IT服务门户

One click installation of highly available Nacos clusters in rainbow

Arm GIC (IV) GIC V3 register class analysis notes.

Data type - integer (C language)

The field value in Splunk subquery fuzzy matching CSV is*

调用华为游戏多媒体服务的创建引擎接口返回错误码1002,错误信息:the params is error

如何在快应用中实现滑动操作组件

Practice of combining rook CEPH and rainbow, a cloud native storage solution

Rapid integration of authentication services - harmonyos platform
随机推荐
Iptables' state module (FTP service exercise)
A single game with goods increased by 100000, and the rural anchor sold men's clothes on top of the list?
AVL balanced binary search tree
About using CDN based on Kangle and EP panel
Interpreting the practical application of maker thinking and mathematics curriculum
Explore creativity in steam art design
Merge sort and non comparison sort
调用华为游戏多媒体服务的创建引擎接口返回错误码1002,错误信息:the params is error
[kuangbin]专题十五 数位DP
How to integrate app linking services in harmonyos applications
Several ways of lambda used in functions in kotlin (higher-order functions)
Using nocalhost to develop microservice application on rainbow
国标GB28181协议视频平台EasyGBS新增拉流超时配置
grpc、oauth2、openssl、双向认证、单向认证等专栏文章目录
Compilation and linking of programs
为什么要选择云原生数据库
数据中台落地实施之法
Snyk dependency security vulnerability scanning tool
[南京大学]-[软件分析]课程学习笔记(一)-introduction
What is the method of manual wiring in PCB design in 22protel DXP_ Chengdu electromechanical Development Undertaking