zoukankan      html  css  js  c++  java
  • 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

    二、题目难度:中等
    三、题解
    方法一:滑动窗口
    先计算不使用秘密技能的满意顾客数
    然后计算使用秘密技能能够在连续X分钟内不生气使得满意的顾客数
    结果为二者相加
    时间复杂度:O(n)
    空间复杂度:O(1)

    class Solution {
        public int maxSatisfied(int[] customers, int[] grumpy, int X) {
            int n = customers.length;
            int total = 0;
            //不使用秘密技巧能够使得顾客满意的人数
            for(int i=0;i<n;i++){
                if(grumpy[i]==0)
                    total += customers[i];
            }
            //使用秘密技巧能够得到的最大值
            int increase = 0;
            for(int i=0;i<X;i++){
                increase += customers[i] * grumpy[i];
            }
            int maxIncresae = increase;
            for(int i=X;i<n;i++){
                increase += customers[i]*grumpy[i] - customers[i-X]*grumpy[i-X];
                maxIncresae = Math.max(maxIncresae,increase);
            }
            return total + maxIncresae;
        }
    }
    

  • 相关阅读:
    ETCD集群部署 和flanne网络插件通信原理介绍
    prometheus02 nodeexporter部署及使用
    docker容器的存储资源(volume)
    ActionScript 3.0 事件机制小结
    ActionScript 3.0 装饰器模式实例
    固定头和底,中间部分自适应布局
    ActionScript 3.0 MVC模式小实例
    A*算法的Actionscript3.0实例
    [Database]sql server 2008 不允许保存更改,您所做的更改要求删除并重新创建以下表 的解决办法
    在phpstorm中svn的使用
  • 原文地址:https://www.cnblogs.com/ttzz/p/14435069.html
Copyright © 2011-2022 走看看