leetcode_1052. Grumpy Bookstore Owner
2024-09-05 16:49:32
1052. Grumpy Bookstore Owner
https://leetcode.com/problems/grumpy-bookstore-owner/
题意:每个时刻i会有customer[i]个顾客进店,在i时刻店主的情绪是grumpy[i],若grumpy[i]==1则店主脾气暴躁,那么顾客会不满意,否则顾客讲感到满意,店主会一个技巧保证连续X时间内不暴躁,但他只能使用一次。问最多可以有多少顾客满意。
解法:找到一个连续X时间段,使用技巧后,得到的收益最大。计算前缀和,然后依次遍历所有连续X时间段,找到使用技巧能使最多的顾客从不满意变为满意。
class Solution
{
public:
int maxSatisfied(vector<int>& customers, vector<int>& grumpy, int X)
{
vector<int> presum(grumpy.size());
if(grumpy[]==)
presum[]=customers[];
for(int i=;i<grumpy.size();i++)
{
presum[i]=grumpy[i]==?(presum[i-]+customers[i]):presum[i-];
}
int max_sum=presum[X-],last_pos=X-;
for(int i=X;i<grumpy.size();i++)
{
int st_pos = i-X+;
if(max_sum<presum[i]-presum[st_pos-])
{
max_sum=presum[i]-presum[st_pos-];
last_pos=i;
}
}
int res=;
for(int i=;i<grumpy.size();i++)
{
if(grumpy[i]==)
res+=customers[i];
else if(i>=last_pos-X+&&i<=last_pos)
res+=customers[i];
}
return res;
}
};
最新文章
- Eclipse代码格式化规范
- Linux aclocal
- Entity Framework 泛型使用
- 【解决】python2.x版本的Django下admin管理页面css无效
- skynet-源码分析1:目录下的文件整理
- 纳税服务系统【用户模块之使用POI导入excel、导出excel】
- scheduleAtFixedRate 与 scheduleWithFixedDelay 的区别
- java 连接SQL Server
- 一个简单的Loading控件
- Non-negative Matrix Factorization 非负矩阵分解
- linux下文件描述符的查看及分析
- RabbitMQ上手记录–part 6-Shovel
- 京东云擎”本周四推出一键免费安装Discuz论坛
- easyui —— footer
- python的with用法
- Guidelines for Installing Oracle HR Sample Schemas
- CORS 跨域请求
- Java笔记-添加自定义公共类库
- .net的.aspx页面调试方法
- python中scipy学习——随机稀疏矩阵及操作