1052. Grumpy Bookstore Owner**

https://leetcode.com/problems/grumpy-bookstore-owner/

题目描述

Today, the bookstore owner has a store open for customers.length minutes. Every minute, some number of customers (customers[i]) enter the store, and all those customers leave after the end of that minute.

On some minutes, the bookstore owner is grumpy. If the bookstore owner is grumpy on the i-th minute, grumpy[i] = 1, otherwise grumpy[i] = 0. When the bookstore owner is grumpy, the customers of that minute are not satisfied, otherwise they are satisfied.

The bookstore owner knows a secret technique to keep themselves not grumpy for X minutes straight, but can only use it once.

Return the maximum number of customers that can be satisfied throughout the day.

Example 1:

Input: customers = [1,0,1,2,1,1,7,5], grumpy = [0,1,0,1,0,1,0,1], X = 3
Output: 16
Explanation: The bookstore owner keeps themselves not grumpy for the last 3 minutes.
The maximum number of customers that can be satisfied = 1 + 1 + 1 + 1 + 7 + 5 = 16.

Note:

  • 1 <= X <= customers.length == grumpy.length <= 20000
  • 0 <= customers[i] <= 1000
  • 0 <= grumpy[i] <= 1

C++ 实现 1

滑动窗口. 统计在大小为 X 的窗口中, 有多少顾客刚好处在店主脾气不好的时刻, 即 grumpy[i] == 1. 其中 grumpy[i] == 0 对应的顾客始终是满意的, 使用 base 来统计. 而对于那些 grumpy[i] == 1 的顾客, 只有在他们刚好在滑动窗口中, 才能满意, 用 new_satisfied 统计在滑动窗口中新满意的顾客, 在窗口滑动过程中使用 max_satisfied 来记录最大值. 最后返回 base + max_satisfied.

class Solution {public:int maxSatisfied(vector<int>& customers, vector<int>& grumpy, int X) {int res = 0, base = 0, new_satisfied = 0, max_satisfied = 0;for (int i = 0; i < customers.size(); ++ i) {if (grumpy[i] == 0) base += customers[i];else new_satisfied += customers[i];if (i + 1 > X) { // 如果 nums[0...i] 范围内的数据个数大于 Xnew_satisfied -= grumpy[i - X] * customers[i - X];}max_satisfied = std::max(max_satisfied, new_satisfied);}return base + max_satisfied;}
};

1052. Grumpy Bookstore Owner**相关推荐

  1. LeetCode Algorithm 1052. 爱生气的书店老板

    1052. 爱生气的书店老板 Ideas 找到一个时间长度为 X 的窗口包含最多原本因为老板生气而不满意的顾客(Sliding Window) ans = 老板不生气时间内的顾客总数(customer ...

  2. 暑期集训3:几何基础 练习题G: HDU - 1052

    2018学校暑期集训第三天--几何基础 练习题G  --   HDU - 1052   (昨天加练题) Tian Ji -- The Horse Racing Here is a famous sto ...

  3. 关于owner group others的测试

    第五章 Linux的文件权限与目录配置 命令总结 关于owner group others的测试 在创建的dennisA用户下创建a.txt [dennisA@localhost ~]$ ls -l ...

  4. 2013-2014 ACM-ICPC, NEERC, Southern Subregional Contest Problem D. Grumpy Cat 交互题

    Problem D. Grumpy Cat 题目连接: http://www.codeforces.com/gym/100253 Description This problem is a littl ...

  5. 中蒙最大陆路口岸2018年运行中欧班列1052列

    资料图:中欧班列 吴正琪 摄 中新社二连浩特1月16日电 (记者 李爱平)二连海关16日晚间对外披露,作为中蒙最大陆路口岸的二连浩特口岸2018年全年共运行中欧班列1052列. 官方消息指,二连海关日 ...

  6. 分享一个BookStore ios程序的例子

    经过4天的开发,ios的网络书店基本完成了: 登陆注册 图书列表,下拉放开刷新,下拉到底查看更多,图片异步加载 我的信息,查看订单修改密码 查看图书详情 加入购物车 提交订单 sqllite存放浏览日 ...

  7. HDU 1052 Tian Ji -- The Horse Racing

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1052 题意:田忌赛马,贪心 #include<iostream> #include<al ...

  8. 在Foreda8上安装libaio-0.3.105-2.i386.rpm

    libaio-0.3.105-2.i386.rpm是安装MySql必须的包,可以从这里下载:http://pan.baidu.com/share/link?shareid=2348086735& ...

  9. 【Groovy】闭包 Closure ( 闭包中调用 Groovy 脚本中的方法 | owner 与 delegate 区别 | 闭包中调用对象中的方法 )

    文章目录 一.闭包中调用 Groovy 脚本中的方法 二.owner 与 delegate 区别 三.闭包中调用 Groovy 对象中的方法 一.闭包中调用 Groovy 脚本中的方法 在 Groov ...

最新文章

  1. 我把帮带份饭的信息错发给导师后.......
  2. 机器学习入门学习笔记:(2.2)线性回归python程序实现
  3. nginx fastcgi python_Nginx + webpy 和FastCGI搭建webpy环境
  4. GoogLeNet——CNN经典网络模型详解(pytorch实现)
  5. Java内存泄露8种情况的总结
  6. 第 197 章 Unity
  7. 中国抗生素骨水泥行业市场供需与战略研究报告
  8. PHP在Linux上上传文件的注意事项
  9. 奚恺元《别做傻瓜做的事》
  10. 【深入理解JS核心技术】14. 什么是一元函数
  11. 分享一个非常强大且好用的绘图控件QCustomPlot
  12. U盘引导网络安装CentOS 7
  13. 飞信linux下载文件,在Linux上安装飞信
  14. Win32Forth初学者指导-前言
  15. easyUI详解教程
  16. Android使用Service播放音乐
  17. R语言条件逻辑回归-因变量是配对资料
  18. android微信支付吊不起微信,安卓系统微信支付失败原因
  19. 一次500行SQL的优化
  20. vb treeview 展开子节点_电路的一般分析法(01)—节点电压法及其算例

热门文章

  1. 每日算法-Excel表列序号
  2. Centos下安装Jexus、.net,上传部署运行asp.net项目
  3. 转转AB平台的设计与实现
  4. 【WinForm】用ChromiumWeb内核开发一个自用的浏览器
  5. hello world 小案例 记事本
  6. IDE中maven依赖jar包报错的处理
  7. 习题4-11 兔子繁衍问题 (详解)(15 分)
  8. 利用ffmpeg 解码ps网络流视频
  9. python 中文数字转换为阿拉伯数字
  10. Codeforces 629C 简单DP