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

原文地址:https://www.cnblogs.com/willwuss/p/12546080.html

时间: 2024-10-26 03:05:15

Leetcode 1052 Grumpy Bookstore Owner. (滑动窗口)的相关文章

leetcode_1052. Grumpy Bookstore Owner

1052. Grumpy Bookstore Owner https://leetcode.com/problems/grumpy-bookstore-owner/ 题意:每个时刻i会有customer[i]个顾客进店,在i时刻店主的情绪是grumpy[i],若grumpy[i]==1则店主脾气暴躁,那么顾客会不满意,否则顾客讲感到满意,店主会一个技巧保证连续X时间内不暴躁,但他只能使用一次.问最多可以有多少顾客满意. 解法:找到一个连续X时间段,使用技巧后,得到的收益最大.计算前缀和,然后依次

[leetcode]239. Sliding Window Maximum滑动窗口最大值

Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position. Return the max sliding

leetcode的Hot100系列--3. 无重复字符的最长子串--滑动窗口

可以先想下这两个问题: 1.怎样使用滑动窗口? 2.如何快速的解决字符查重问题? 滑动窗口 可以想象一下有两个指针,一个叫begin,一个叫now 这两个指针就指定了当前正在比较无重复的字符串,当再往后读取一个字符的时候,就需要比较该字符在begin到now之间是否有重复,如果有重复的话,则记录当前字符串长度,然后把begin往后移动,继续寻找后面的无重复字符子串. 例如,这里的字符串是:"fabcade". 1.当开始比较字符串的时候,begin指向了第一个字符f,now也指向了第一

[滑动窗口] leetcode 1004 Max Consecutive Ones III

problem:https://leetcode.com/problems/max-consecutive-ones-iii/ 维护最多包含k个0的滑动窗口,一旦超过了k个0,把队首的0 pop出来.不断更新当前滑动窗口中的数据个数,并取最大值返回即可. class Solution { public: int longestOnes(vector<int>& A, int K) { int count = 0; int index = -1; deque<int> zer

[滑动窗口] leetcode 424 Longest Repeating Character Replacement

problem:https://leetcode.com/problems/longest-repeating-character-replacement/ 维护一个最多包含k个额外字符的滑动窗口.需要记录当前出现次数最多字符的出现次数来判断窗口是否合法,如果超过了,就把首指针向后挪一位,同时更新最多出现次数.对每个合法窗口,取其中的最大值. class Solution { public: int characterReplacement(string s, int k) { int begi

leetcode 239. 滑动窗口最大值(单调队列)

给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧.你只可以看到在滑动窗口内的 k 个数字.滑动窗口每次只向右移动一位. 返回滑动窗口中的最大值. 进阶: 你能在线性时间复杂度内解决此题吗? 示例: 输入: nums = [1,3,-1,-3,5,3,6,7], 和 k = 3输出: [3,3,5,5,6,7] 解释: 滑动窗口的位置 最大值--------------- -----[1 3 -1] -3 5 3 6 7 3 1 [3 -1 -3] 5 3 6

leetcode ---双指针+滑动窗口

一:Minimum Size Subarray Sum 题目: Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead. For example, given the array [2,3,1,2,4,3] and s = 7, th

leetcode 3 Longest Substring Without Repeating Characters(滑动窗口)

用滑动窗口的思想来做.用一个unordered_map来查询之前的char有没有在现在的窗口中. class Solution { public: int lengthOfLongestSubstring(string s) { unordered_map<char,int>mp; int ans=0,now=0;//now is the window's instant length int b=0,e=0;//the window's begin and end int len=s.len

[leetcode]346. Moving Average from Data Stream滑动窗口平均值

Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window. For example, MovingAverage m = new MovingAverage(3); m.next(1) = 1 m.next(10) = (1 + 10) / 2 m.next(3) = (1 + 10 + 3) / 3 m.next(5) = (1