# 【基础篇 - Day 29】 2020-11-29 - 1052. 爱生气的书店老板

# 题目描述

# 入选理由

熟悉滑动窗口

# 题目描述

今天,书店老板有一家店打算试营业 customers.length 分钟。每分钟都有一些顾客(customers[i])会进入书店,所有这些顾客都会在那一分钟结束后离开。

在某些时候,书店老板会生气。 如果书店老板在第 i 分钟生气,那么 grumpy[i] = 1,否则 grumpy[i] = 0。 当书店老板生气时,那一分钟的顾客就会不满意,不生气则他们是满意的。

书店老板知道一个秘密技巧,能抑制自己的情绪,可以让自己连续 X 分钟不生气,但却只能使用一次。

请你返回这一天营业下来,最多有多少客户能够感到满意的数量。

示例:

输入:customers = [1,0,1,2,1,1,7,5], grumpy = [0,1,0,1,0,1,0,1], X = 3
输出:16
解释:
书店老板在最后 3 分钟保持冷静。
感到满意的最大客户数量 = 1 + 1 + 1 + 1 + 7 + 5 = 16.


提示:

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

来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/grumpy-bookstore-owner 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

# 我的回答

# 解法一

# 时空复杂度

时间复杂度:O(n)

空间复杂度: O(1)

昨天睡觉前脑子糊涂了,思路对的一直写不出来。。。

var maxSatisfied = function (customers, grumpy, X) {
    // 记录每个X长度的合
    let count = 0
    // 取每个X长度中最大的
    let max = 0
    grumpy = grumpy.map((v, i) => v * customers[i])
    //满意的顾客
    let yes = 0
    for (let i = 0; i < customers.length; i++) {
        if (!grumpy[i]) yes += customers[i]
        count += grumpy[i]
        i >= X && (count -= grumpy[i - X])
        max = Math.max(max, count)
    }
    return yes + max
};

# 参考回答

Last Updated: 12/22/2022, 9:53:26 AM