当前位置:网站首页>LeetCode 135. 分发糖果
LeetCode 135. 分发糖果
2022-08-04 06:42:00 【HumbleFool】

贪心
class Solution {
public:
int candy(vector<int>& ratings) {
int n = ratings.size();
vector<int> left(n, 1);
vector<int> right(n, 1);
for(int i = 1; i < n; i ++)
if(ratings[i] > ratings[i - 1])
left[i] += left[i - 1];
for(int i = n - 2; i >= 0; i --)
if(ratings[i] > ratings[i + 1])
right[i] += right[i + 1];
int res = 0;
for(int i = 0; i < n; i ++)
res += max(left[i], right[i]);
return res;
}
};
边栏推荐
猜你喜欢
随机推荐
使用腾讯云发送短信 ---- 手把手教你搞定所有步骤
错误记录:TypeError: object() takes no parameters
Error ER_NOT_SUPPORTED_AUTH_MODE Client does not support authentication protocol requested by serv
异常值 识别与处理方法
分布式计算实验3 基于PRC的书籍信息管理系统
[Paper Notes] - Low Illumination Image Enhancement - Supervised - RetinexNet - 2018-BMVC
Triton部署mmdeploy导出的TensorRT模型失败篇
adb无法桥接夜神模拟器
MySQL基础(DDL、DML、DQL)
SQL如何从字符串截取指定字符(LEFT、MID、RIGHT三大函数)
matlab科研绘图模板,直接奉上源代码!
舍不得花钱买1stOpt,不妨试试这款免费的拟合优化神器【openLU】
打破千篇一律,DIY属于自己独一无二的商城
Mac安装PHP开发环境
Sql优化总结!详细!(2021最新面试必问)
The national vocational skills contest competition of network security emergency response
LeetCode(剑指 Offer)- 18. 删除链表的节点
idea使用@Autowired注解爆红原因及解决方法
函数柯里化详解
用手机也能轻松玩转MATLAB编程









