当前位置:网站首页>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, two-way authentication, one-way authentication and other column directories
- 如何在快应用中实现滑动操作组件
- Rapid integration of authentication services - harmonyos platform
- Input and output of floating point data (C language)
- 如何理解分布式架构和微服务架构呢
- Deit learning notes
- Iptables' state module (FTP service exercise)
- 调用华为游戏多媒体服务的创建引擎接口返回错误码1002,错误信息:the params is error
- Splunk query CSV lookup table data dynamic query
- 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"
猜你喜欢
iptables 之 state模块(ftp服务练习)
A method for quickly viewing pod logs under frequent tests (grep awk xargs kuberctl)
Implement your own dataset using bisenet
The single value view in Splunk uses to replace numeric values with text
Installation and configuration of PLSQL
The field value in Splunk subquery fuzzy matching CSV is*
Using helm to install rainbow in various kubernetes
路由信息协议——RIP
如何在HarmonyOS应用中集成App Linking服务
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"
随机推荐
【无标题】
Deit learning notes
2-3 lookup tree
说一个软件创业项目,有谁愿意投资的吗?
Opencv learning notes II - basic image operations
【微信小程序:缓存操作】
[Yu Yue education] higher vocational English reference materials of Nanjing Polytechnic University
Data type - floating point (C language)
A single game with goods increased by 100000, and the rural anchor sold men's clothes on top of the list?
Learn how to compile basic components of rainbow from the source code
21 general principles of wiring in circuit board design_ Provided by Chengdu circuit board design
Mock.js用法详解
Splunk query CSV lookup table data dynamic query
MES系统,是企业生产的必要选择
Rainbow version 5.6 was released, adding a variety of installation methods and optimizing the topology operation experience
测试踩坑 - 当已有接口(或数据库表中)新增字段时,都需要注意哪些测试点?
Open3d ISS key points
Practice of implementing cloud native Devops based on rainbow library app
idea里使用module项目的一个bug
Rainbow 5.7.1 supports docking with multiple public clouds and clusters for abnormal alarms