当前位置:网站首页>1052. The Angry Bookstore Boss
1052. The Angry Bookstore Boss
2022-08-08 14:31:00 【Xiuqiang】
1052. 爱生气的书店老板
#滑动窗口
有一个书店老板,他的书店开了 n 分钟.每分钟都有一些顾客进入这家商店.给定一个长度为 n 的整数数组 customers ,其中 customers[i] 是在第 i 分钟开始时进入商店的顾客数量,所有这些顾客在第 i 分钟结束后离开.
在某些时候,书店老板会生气. 如果书店老板在第 i 分钟生气,那么 grumpy[i] = 1,否则 grumpy[i] = 0.
当书店老板生气时,那一分钟的顾客就会不满意,若boss is not angry则顾客是满意的.
书店老板知道一个秘密技巧,能抑制自己的情绪,可以让自己连续 minutes 分钟不生气,但却只能使用一次.
请你返回 这一天营业下来,最多有多少客户能够感到满意 .
示例 1:
输入:customers = [1,0,1,2,1,1,7,5], grumpy = [0,1,0,1,0,1,0,1], minutes = 3
输出:16
解释:书店老板在最后 3 分钟保持冷静.
感到满意的最大客户数量 = 1 + 1 + 1 + 1 + 7 + 5 = 16.
示例 2:
输入:customers = [1], grumpy = [0], minutes = 1
输出:1
提示:
n == customers.length == grumpy.length
1 <= minutes <= n <= 2 * 104
0 <= customers[i] <= 1000
grumpy[i] == 0 or 1
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/grumpy-bookstore-owner
解法:滑动窗口
class Solution {
public int maxSatisfied(int[] customers, int[] grumpy, int minutes) {
// When the boss has no skills,The number of satisfied customers
int n = 0;
int length = customers.length;
for (int i = 0; i < length; i++) {
int p = customers[i];
// boss is not angry
if (grumpy[i] == 0) {
n += p;
}
// The boss activates the skill right from the start,比如 minutes 是 3,那么 0 1 2 Customers in minutes will be satisfied(Because the boss used skills,不会生气)
if (i < minutes && grumpy[i] == 1) {
n += p;
}
}
// max is the maximum number of customers that can be satisfied,The initial value is that the boss activates the skill at the beginning
int max = n;
// 滑动窗口,From the first activation of the skill to the end
for (int i = minutes; i < length; i++) {
// Each length is minutes Each time the sliding window is moved to the right,The minute the far left slides out of the window,If the boss is angry,Just subtract the number of customers that minute from the number of satisfied customers.
// Slide in from the window
if (grumpy[i] == 1) {
n += customers[i];
}
// Slide out of the window
if (grumpy[i - minutes] == 1) {
n -= customers[i - minutes];
}
max = n > max ? n : max;
}
return max;
}
}
作者:xiu-qiang-jiang
链接:https://leetcode.cn/problems/grumpy-bookstore-owner/solution/by-xiu-qiang-jiang-foid/
边栏推荐
猜你喜欢
如何成为团队核心?从写出不可维护的代码开始
机器学习+深度学习笔记(持续更新~)
KD-SCFNet: More Accurate and Efficient Salient Object Detection Through Knowledge Distillation (ECCV2022)
JS-BOM-通过或运算-可以实现缺省值
bandanas Kerchief头巾是何含义?
a += 1 += 1为什么是错的?
【控制】动力学建模简介 --> 牛顿-欧拉 (Newton-Euler) 法和拉格朗日 (Lagrange) 法
更改C盘用户目录下的用户名(亲测有效)
PHP —— 用 ThinkPHP5.0 实现微信小程序登陆
从零开始,如何拥有自己的博客网站【华为云至简致远】
随机推荐
【小码匠自习室】CSP-J/S复试高分秘诀经验分享
超详细的最新版 2022.2 kali 安装步骤及拍摄快照的方法
浅学一下二叉树链式存储结构的遍历
Ingress:比Service更强大的服务暴露与负载均衡
30K成功入职京东:拿到京东offer经验分享「面试经历+面试真题」
【小码匠自习室】朋友的朋友不是朋友
JS-BOM-名字转换器-输入名字位置颠倒
Tsinghua | GLM-130B: An Open Bilingual Pre-training Model
【系统设计】S3 对象存储
跟我一起了解云耀云服务器HECS【华为云至简致远】
LeetCode 每日一题 2022/8/1-2022/8/7
设计一个跨平台的即时通讯系统(采用华为云ECS服务器作为服务端 )【华为云至简致远】
【Kaggle实践记录】电商图片分类
【低代码】1405- 浅谈低代码平台远程组件加载方案
看三年的CRUD程序员如何解决数据库死锁的
Time to update your tech arsenal in 2020: Asgi vs Wsgi (FastAPI vs Flask)
PHP —— 用 ThinkPHP5.0 实现微信小程序登陆
现在网上开户安全么?接着证券开户选择哪个证券?
2022年8月7日 暑假第四周总结
【小码匠自习室】叛逆的小孩,打死也不改