美文网首页快乐写代码
T1052、爱生气的书店老板

T1052、爱生气的书店老板

作者: 上行彩虹人 | 来源:发表于2020-08-26 19:35 被阅读0次

今天,书店老板有一家店打算试营业 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.

temp数组的值为customers和grumpy对应值得乘积,表示该时间生气值的损失。所以问题转化为求temp数组K宽口内的最大子序列。

  public int maxSatisfied(int[] customers, int[] grumpy, int X) {
        int res = 0;
        if(customers == null || grumpy == null)
            return 0;
        if(customers.length == 1)
            if(X >= 1)
                return customers[0];
            else
                return customers[0] * (1 - grumpy[0]);
        int[] temp = new int[customers.length];
        for(int i = 0; i < customers.length; i++){
            temp[i] = customers[i] * grumpy[i];
        }
        int i = 0, j = 0;
        int max = 0;
        while(j < X){
            max += temp[j];
            j++;
        }
            
        int l = i, r = j-1;
        int t = max; //记录窗口的值
        while(j < customers.length){
            t = t - temp[i] + temp[j];
            if(t > max){
                max = t;
                l = i+1;
                r = j;
            }
            i++; j++;
        }
        while(l <= r){
            grumpy[l] = 0;
            l++;
        }
        for(int p = 0; p < customers.length; p++)
            if(grumpy[p] == 0)
                res += customers[p];
        
        return res;
    }

相关文章

  • T1052、爱生气的书店老板

    今天,书店老板有一家店打算试营业 customers.length 分钟。每分钟都有一些顾客(customers[...

  • LeetCode #1052 Grumpy Bookstore

    1052 Grumpy Bookstore Owner 爱生气的书店老板 Description:There is...

  • 2021.2.23每日一题

    1052. 爱生气的书店老板[https://leetcode-cn.com/problems/grumpy-bo...

  • Leetcode 爱生气的书店老板

    题目描述 leecode第1052题:爱生气的书店老板[https://leetcode-cn.com/probl...

  • leetcode1052 爱生气的书店老板

    题目 分析 窗口大小固定的滑动窗口问题。 代码

  • 1052. 爱生气的书店老板(Python)

    难度:★★★☆☆类型:数组方法:滑动窗口 题目 力扣链接请移步本题传送门[https://leetcode-cn....

  • IOS 算法(中级篇) ----- 爱生气的书店老板

    今天,书店老板有一家店打算试营业 customers.length 分钟。每分钟都有一些顾客(customers[...

  • 书店的老板

    书店的老板,他是一个创业失败的人。三十岁之前他一直在外面闯荡江湖,三十岁之后他选择了回到自己的故乡安居,经营...

  • 书店老板

    世间 那么有趣 你呢 万般无聊 为何 闷闷不乐 你看 花花世界 我想 做一个书店老板 还要 有一个知己就够 这又是...

  • 书店老板

    一:公园冬季闭园,但在夏天,你真的能在那里找到独角兽? 二:我不相信有上帝,我没有宗教信仰,但这家书店对我来说,是...

网友评论

    本文标题:T1052、爱生气的书店老板

    本文链接:https://www.haomeiwen.com/subject/qimfsktx.html