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;
}
};

最新文章

  1. Eclipse代码格式化规范
  2. Linux aclocal
  3. Entity Framework 泛型使用
  4. 【解决】python2.x版本的Django下admin管理页面css无效
  5. skynet-源码分析1:目录下的文件整理
  6. 纳税服务系统【用户模块之使用POI导入excel、导出excel】
  7. scheduleAtFixedRate 与 scheduleWithFixedDelay 的区别
  8. java 连接SQL Server
  9. 一个简单的Loading控件
  10. Non-negative Matrix Factorization 非负矩阵分解
  11. linux下文件描述符的查看及分析
  12. RabbitMQ上手记录–part 6-Shovel
  13. 京东云擎”本周四推出一键免费安装Discuz论坛
  14. easyui —— footer
  15. python的with用法
  16. Guidelines for Installing Oracle HR Sample Schemas
  17. CORS 跨域请求
  18. Java笔记-添加自定义公共类库
  19. .net的.aspx页面调试方法
  20. python中scipy学习——随机稀疏矩阵及操作

热门文章

  1. 利用python数据分析panda学习笔记之Series
  2. 取消IE增强的安全配置
  3. MessageFomat学习
  4. C#总结---方法的out参数和ref参数
  5. Lightoj1012【DFS】
  6. laravel定义全局变量并输出(实例)
  7. unity3d easytouch教程
  8. Faster_Rcnn在windows下运行踩坑总结
  9. [題解]luogu_P2055假期的宿舍(二分圖最大匹配)
  10. C# 基础之类的实例化