zoukankan      html  css  js  c++  java
  • Leetcode 1052 Grumpy Bookstore Owner. (滑动窗口)

    Leetcode 1052

    问题描述

    Today, the bookstore owner has a store open for customers.length minutes.  Every minute, some number of customers (customers[i]) enter the store, and all those customers leave after the end of that minute.
    
    On some minutes, the bookstore owner is grumpy.  If the bookstore owner is grumpy on the i-th minute, grumpy[i] = 1, otherwise grumpy[i] = 0.  When the bookstore owner is grumpy, the customers of that minute are not satisfied, otherwise they are satisfied.
    
    The bookstore owner knows a secret technique to keep themselves not grumpy for X minutes straight, but can only use it once.
    
    Return the maximum number of customers that can be satisfied throughout the day.
    

    例子

    Input: customers = [1,0,1,2,1,1,7,5], grumpy = [0,1,0,1,0,1,0,1], X = 3
    Output: 16
    Explanation: The bookstore owner keeps themselves not grumpy for the last 3 minutes. 
    The maximum number of customers that can be satisfied = 1 + 1 + 1 + 1 + 7 + 5 = 16.
    

    方法

      1. 使用滑动窗口windows记录不满意的客户数(X分钟)。当滑动窗的宽度大于X时从滑动窗的左端减去不满意的客户
        windows -= grumpy[i - X] * customers[i - X];
      2. 使用satisfied记录satistified客户数量没有脾气暴躁的技术;
      3. 在迭代结束时,satisfied+ max(winOfMakeSatisfied)是答案。

    ** Solution Java **
    ** 5ms, beats 24.57% **
    ** 52.9MB, beats 100.00% **
    class Solution {
        public int maxSatisfied(int[] customers, int[] grumpy, int X) {
            int satisfied = 0, maxMakeSatisfied = 0, windows = 0;
            for (int i = 0; i < customers.length; ++i) {
                if (grumpy[i] == 0) 
                    satisfied += customers[i];
                else
                    windows += customers[i];
                if (X <= i) {
                    windows -= grumpy[i - X] * customers[i - X];
                }
                maxMakeSatisfied = Math.max(maxMakeSatisfied, windows);
            }
            return satisfied + maxMakeSatisfied;
        }
    }
    
    ** Solution Python3 **
    ** 320ms, beats 42.52% **
    ** 15MB, beats 100.00% **
    class Solution:
        def maxSatisfied(self, customers: List[int], grumpy: List[int], X: int) -> int:
            i = win_of_make_satisfied = satisfied = max_make_satisfied = 0
            for c, g in zip(customers, grumpy):
                satisfied += (1 - g) * c
                win_of_make_satisfied += g * c
                if i >= X:
                    win_of_make_satisfied -= grumpy[i - X] * customers[i - X]
                max_make_satisfied = max(win_of_make_satisfied, max_make_satisfied)  
                i += 1    
            return satisfied + max_make_satisfied
    
  • 相关阅读:
    07java基础知识
    06java基础知识
    我们都忽略了Html5的力量,如果只看成一种技术就大错特错了!
    “微信应用号对行业影响”之一,app开发速来围观
    App开发中甲乙方冲突会闹出啥后果?H5 APP 开发可以改变现状吗
    开发APP不搞清楚这20个问题,必然沦为一场灾难
    H5 App设计者需要注意的21条禁忌
    H5 APP开发必读,20个你不知道的Html5新特征和窍门
    H5 App如此强悍,要降薪的恐怕已不只是iOS程序员
    关于APP,原生和H5开发技术的争论
  • 原文地址:https://www.cnblogs.com/willwuss/p/12546080.html
Copyright © 2011-2022 走看看