列表

详情


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

 

提示:

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
class Solution { public: int maxSatisfied(vector<int>& customers, vector<int>& grumpy, int minutes) { } };

python3 解法, 执行用时: 65 ms, 内存消耗: 18.1 MB, 提交时间: 2024-04-23 13:00:37

class Solution:
    def maxSatisfied(self, customers: List[int], grumpy: List[int], minutes: int) -> int:
        s = [0, 0]
        max_s1 = 0
        for i, (c, g) in enumerate(zip(customers, grumpy)):
            s[g] += c
            if i < minutes - 1:  # 窗口长度不足 minutes
                continue
            max_s1 = max(max_s1, s[1])
            if grumpy[i - minutes + 1]:
                s[1] -= customers[i - minutes + 1]  # 窗口最左边元素离开窗口
        return s[0] + max_s1

golang 解法, 执行用时: 15 ms, 内存消耗: 6.2 MB, 提交时间: 2024-04-23 13:00:21

func maxSatisfied(customers, grumpy []int, minutes int) int {
    s := [2]int{}
    maxS1 := 0
    for i, c := range customers {
        s[grumpy[i]] += c
        if i < minutes-1 { // 窗口长度不足 minutes
            continue
        }
        maxS1 = max(maxS1, s[1])
        if grumpy[i-minutes+1] > 0 {
            s[1] -= customers[i-minutes+1] // 窗口最左边元素离开窗口
        }
    }
    return s[0] + maxS1
}

rust 解法, 执行用时: 2 ms, 内存消耗: 2.3 MB, 提交时间: 2024-04-23 13:00:09

impl Solution {
    pub fn max_satisfied(customers: Vec<i32>, grumpy: Vec<i32>, minutes: i32) -> i32 {
        let k = minutes as usize;
        let mut s = [0, 0];
        let mut max_s1 = 0;
        for (i, (&c, &g)) in customers.iter().zip(grumpy.iter()).enumerate() {
            s[g as usize] += c;
            if i < k - 1 {  // 窗口长度不足 minutes
                continue;
            }
            max_s1 = max_s1.max(s[1]);
            if grumpy[i - k + 1] == 1 {
                s[1] -= customers[i - k + 1]; // 窗口最左边元素离开窗口
            }
        }
        s[0] + max_s1
    }
}

javascript 解法, 执行用时: 76 ms, 内存消耗: 52 MB, 提交时间: 2024-04-23 12:59:33

/**
 * @param {number[]} customers
 * @param {number[]} grumpy
 * @param {number} minutes
 * @return {number}
 */
var maxSatisfied = function(customers, grumpy, minutes) {
    const s = [0, 0];
    let maxS1 = 0;
    for (let i = 0; i < customers.length; i++) {
        s[grumpy[i]] += customers[i];
        if (i < minutes - 1) { // 窗口长度不足 minutes
            continue;
        }
        maxS1 = Math.max(maxS1, s[1]);
        // 窗口最左边元素离开窗口
        s[1] -= grumpy[i - minutes + 1] ? customers[i - minutes + 1] : 0;
    }
    return s[0] + maxS1;
};

python3 解法, 执行用时: 64 ms, 内存消耗: 16.7 MB, 提交时间: 2022-12-05 16:30:37

class Solution:
    def maxSatisfied(self, customers: List[int], grumpy: List[int], minutes: int) -> int:
        n = len(customers)
        total = sum(c for c, g in zip(customers, grumpy) if g == 0)
        maxIncrease = increase = sum(c * g for c, g in zip(customers[:minutes], grumpy[:minutes]))
        for i in range(minutes, n):
            increase += customers[i] * grumpy[i] - customers[i - minutes] * grumpy[i - minutes]
            maxIncrease = max(maxIncrease, increase)
        return total + maxIncrease

golang 解法, 执行用时: 44 ms, 内存消耗: 6.4 MB, 提交时间: 2022-12-05 16:30:03

func maxSatisfied(customers []int, grumpy []int, minutes int) int {
    total := 0
    n := len(customers)
    for i := 0; i < n; i++ {
        if grumpy[i] == 0 {
            total += customers[i]
        }
    }
    increase := 0
    for i := 0; i < minutes; i++ {
        increase += customers[i] * grumpy[i]
    }
    maxIncrease := increase
    for i := minutes; i < n; i++ {
        increase = increase - customers[i-minutes]*grumpy[i-minutes] + customers[i]*grumpy[i]
        maxIncrease = max(maxIncrease, increase)
    }
    return total + maxIncrease
}

func max(a, b int) int {
    if a > b {
        return a
    }
    return b
}

javascript 解法, 执行用时: 68 ms, 内存消耗: 43.4 MB, 提交时间: 2022-12-05 16:29:48

/**
 * @param {number[]} customers
 * @param {number[]} grumpy
 * @param {number} minutes
 * @return {number}
 */
var maxSatisfied = function(customers, grumpy, minutes) {
    let total = 0;
    const n = customers.length;
    for (let i = 0; i < n; i++) {
        if (grumpy[i] === 0) {
            total += customers[i];
        }
    }
    let increase = 0;
    for (let i = 0; i < minutes; i++) {
        increase += customers[i] * grumpy[i];
    }
    let maxIncrease = increase;
    for (let i = minutes; i < n; i++) {
        increase = increase - customers[i - minutes] * grumpy[i - minutes] + customers[i] * grumpy[i];
        maxIncrease = Math.max(maxIncrease, increase);
    }
    return total + maxIncrease;
};

java 解法, 执行用时: 2 ms, 内存消耗: 44.6 MB, 提交时间: 2022-12-05 16:29:31

class Solution {
    public int maxSatisfied(int[] customers, int[] grumpy, int minutes) {
        int total = 0;
        int n = customers.length;
        for (int i = 0; i < n; i++) {
            if (grumpy[i] == 0) {
                total += customers[i];
            }
        }
        int increase = 0;
        for (int i = 0; i < minutes; i++) {
            increase += customers[i] * grumpy[i];
        }
        int maxIncrease = increase;
        for (int i = minutes; i < n; i++) {
            increase = increase - customers[i - minutes] * grumpy[i - minutes] + customers[i] * grumpy[i];
            maxIncrease = Math.max(maxIncrease, increase);
        }
        return total + maxIncrease;
    }
}

上一题