当前位置:网站首页>1052. 爱生气的书店老板
1052. 爱生气的书店老板
2022-08-08 14:26:00 【秀强】
1052. 爱生气的书店老板
#滑动窗口
有一个书店老板,他的书店开了 n 分钟。每分钟都有一些顾客进入这家商店。给定一个长度为 n 的整数数组 customers ,其中 customers[i] 是在第 i 分钟开始时进入商店的顾客数量,所有这些顾客在第 i 分钟结束后离开。
在某些时候,书店老板会生气。 如果书店老板在第 i 分钟生气,那么 grumpy[i] = 1,否则 grumpy[i] = 0。
当书店老板生气时,那一分钟的顾客就会不满意,若老板不生气则顾客是满意的。
书店老板知道一个秘密技巧,能抑制自己的情绪,可以让自己连续 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) {
// 老板不用技能时,感到满足的顾客的数量
int n = 0;
int length = customers.length;
for (int i = 0; i < length; i++) {
int p = customers[i];
// 老板不生气
if (grumpy[i] == 0) {
n += p;
}
// 老板一开始就发动技能,比如 minutes 是 3,那么 0 1 2 分钟的顾客都会满足(因为老板用了技能,不会生气)
if (i < minutes && grumpy[i] == 1) {
n += p;
}
}
// max 为最多能满足的顾客的数量,初始值为老板在一开始就发动技能
int max = n;
// 滑动窗口,从第一次发动技能之后开始到最后
for (int i = minutes; i < length; i++) {
// 每次长度为 minutes 的滑动窗口每次向右移动时,最左侧从窗口中滑出的那一分钟,如果老板是生气的,就要把满意的顾客数量减去那一分钟的顾客数量。
// 从窗口滑入
if (grumpy[i] == 1) {
n += customers[i];
}
// 从窗口滑出
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/
边栏推荐
猜你喜欢
随机推荐
华为云会议初体验【华为云至简致远】
用 Antlr 重构脚本解释器
接口测试,
【控制】动力学建模简介 --> 牛顿-欧拉 (Newton-Euler) 法和拉格朗日 (Lagrange) 法
「PHP基础知识」检测数据类型
KD-SCFNet: More Accurate and Efficient Salient Object Detection Through Knowledge Distillation (ECCV2022)
【系统设计】S3 对象存储
shell三剑客-----awk命令
Harvard University smashes the field: DALL-E 2 is just a "glue monster", and the generation accuracy rate is only 22%
Tsinghua | GLM-130B: An Open Bilingual Pre-training Model
进程和线程
浏览器跨域方案,适用于本地调试接口(超简单)
【小码匠自习室】CSP-J/S复试高分秘诀经验分享
俄驻美大使馆:扎波罗热核电站遭炮击威胁欧洲核安全
Full of dry goods, Yu Jingxin class of the Institute of Information Technology, Chinese Academy of Sciences will help you get academic research and thesis writing skills
pip install xxx 出现 AttributeError: ‘tuple’ object has no attribute ‘read’ 错误
机器学习+深度学习笔记(持续更新~)
【小码匠自习室】ABC084 - D:喜欢这样的大神,超有才华
[Small Coder Study Room] ABC179-C: It is a miracle that the code does not count down
【小码匠自习室】[NOI Online 2020-3 入门组] 最急救助:被“幸运女神”眷顾的人