当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢

CAN协议详解-01
![[Paper Notes] - Low Illumination Image Enhancement - Supervised - RetinexNet - 2018-BMVC](/img/54/685fb2620aa53416437943705d3d38.png)
[Paper Notes] - Low Illumination Image Enhancement - Supervised - RetinexNet - 2018-BMVC

小猫爪:AWR294x学习笔记02-AWR294x之DPM&IPC

Detailed ResNet: What problem is ResNet solving?

反射与枚举

简析强制缓存和协商缓存

Activiti 工作流引擎 详解

MySQL面试题大全(陆续更新)

The national vocational skills contest competition of network security emergency response
![玩转TypeScript对象、对象作为参数进行函数传递、接口和内置对象[无敌态]](/img/23/3405b488d11a4700a9b47758214574.png)
玩转TypeScript对象、对象作为参数进行函数传递、接口和内置对象[无敌态]
随机推荐
花了近70美元入手的学生版MATLAB体验到底如何?
对产品设计,架构设计的一点思考
curl (7) Failed connect to localhost8080; Connection refused
MySQL复制表结构、表数据的方法
打破千篇一律,DIY属于自己独一无二的商城
七夕送礼,心愿直抵!
JVM工具之 JPS
如何用matlab做高精度计算?【第二辑】
有人试过用NPGsql驱动连接openGauss开发应用的吗?
Praat:语音标注工具【保存为TextGrid文件】
【并发】概念
LeetCode(剑指 Offer)- 18. 删除链表的节点
MySQL错误-this is incompatible with sql_mode=only_full_group_by完美解决方案
登录拦截实现过程
data:image/jpg;base64格式数据转化为图片
Produce definition 产品与行业分析 勤于思考 善于总结 强于表达
unity webgl报 Uncaught SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON
手把手教你Charles抓包工具使用
关于我写的循环遍历
Mac安装PHP开发环境