zoukankan      html  css  js  c++  java
  • [LeetCode] 1052. Grumpy Bookstore Owner

    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. 

    Example 1:

    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.

    Note:

    • 1 <= X <= customers.length == grumpy.length <= 20000
    • 0 <= customers[i] <= 1000
    • 0 <= grumpy[i] <= 1

    爱生气的书店老板。

    今天,书店老板有一家店打算试营业 customers.length 分钟。每分钟都有一些顾客(customers[i])会进入书店,所有这些顾客都会在那一分钟结束后离开。

    在某些时候,书店老板会生气。 如果书店老板在第 i 分钟生气,那么 grumpy[i] = 1,否则 grumpy[i] = 0。 当书店老板生气时,那一分钟的顾客就会不满意,不生气则他们是满意的。

    书店老板知道一个秘密技巧,能抑制自己的情绪,可以让自己连续 X 分钟不生气,但却只能使用一次。

    请你返回这一天营业下来,最多有多少客户能够感到满意的数量。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/grumpy-bookstore-owner
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    思路是滑动窗口。这个题我们可以这样想,假设老板一直都不生气,那么我们可以使得所有的customer都满意,那么结果自然是customer数组的累加和。但是现在当老板在某些时候会生气的情况下,我们需要找一个长度为X的子数组,能涵盖尽量多的 grumpy[i] = 1 (老板生气的情况)。所以一开始滑动窗口的end指针往前走,如果end指针指向的index那一刻老板是生气的,则记录一个curSum表示目前能挽回的customer数量;当end - start + 1 == X的时候,说明需要缩短窗口了,此时如果start指针指向的下标老板是生气的,我们则需要把这些customer还回去,因为他们不在窗口里了,也就无法挽回了。

    时间O(n)

    空间O(1)

    Java实现

     1 class Solution {
     2     public int maxSatisfied(int[] customers, int[] grumpy, int X) {
     3         int satisfied = 0;
     4         int len = grumpy.length;
     5         // 不生气的时候的顾客总数
     6         for (int i = 0; i < len; i++) {
     7             if (grumpy[i] == 0) {
     8                 satisfied += customers[i];
     9             }
    10         }
    11 
    12         int start = 0;
    13         int end = 0;
    14         int curSum = 0;
    15         int max = 0;
    16         while (end < len) {
    17             if (grumpy[end] == 1) {
    18                 curSum += customers[end];
    19             }
    20             // if (end - start + 1 >= X)
    21             if (end - start + 1 == X) {
    22                 max = Math.max(max, curSum);
    23                 if (grumpy[start] == 1) {
    24                     curSum -= customers[start];
    25                 }
    26                 start++;
    27             }
    28             end++;
    29         }
    30         return satisfied + max;
    31     }
    32 }

    sliding window相关题目

    LeetCode 题目总结

  • 相关阅读:
    Android--adb
    Android 爬坑之路
    Android倒计时实现
    Android Studio常用设置
    Java Web开发——MySQL数据库的安装与配置
    DOS命令(系统错误5,拒绝访问)的解决方法
    Java EE开发环境——MyEclipse2017破解 和 Tomcat服务器配置
    设计模式-工厂模式
    设计模式-简单工厂模式
    设计模式简介
  • 原文地址:https://www.cnblogs.com/cnoodle/p/14439083.html
Copyright © 2011-2022 走看看