[LeetCode] 1052. Grumpy Bookstore Owner

There is a bookstore owner that has a store open for n minutes. Every minute, some number of customers enter the store. You are given an integer array customers of length n where customers[i] is the number of the customer that enters the store at the start of the ith minute and all those customers leave after the end of that minute.

On some minutes, the bookstore owner is grumpy. You are given a binary array grumpy where grumpy[i] is 1 if the bookstore owner is grumpy during the ith minute, and is 0 otherwise.

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 minutes consecutive minutes, 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], minutes = 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.

Example 2:

Input: customers = [1], grumpy = [0], minutes = 1
Output: 1

Constraints:

  • n == customers.length == grumpy.length
  • 1 <= minutes <= n <= 2 * 104
  • 0 <= customers[i] <= 1000
  • grumpy[i] is either 0 or 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 == X 的时候,说明需要缩短窗口了,此时如果 start 指针指向的下标老板是生气的,我们则需要把这些 customer 还回去,因为他们不在窗口里了,也就无法挽回了。

时间O(n)

空间O(1)

Java实现

 1 class Solution {
 2     public int maxSatisfied(int[] customers, int[] grumpy, int minutes) {
 3         int satisfied = 0;
 4         int n = customers.length;
 5         // 老板完全不生气的时候,顾客的满意度
 6         for (int i = 0; i < n; 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 < n) {
17             if (grumpy[end] == 1) {
18                 curSum += customers[end];
19             }
20             end++;
21             // 如果长度正好等于minutes,统计当前max的值,代表能有多少顾客的满意度可以被改变
22             if (end - start == minutes) {
23                 max = Math.max(max, curSum);
24                 if (grumpy[start] == 1) {
25                     curSum -= customers[start];
26                 }
27                 start++;
28             }
29         }
30         return satisfied + max;
31     }
32 }

 

另外一种实现,时间空间复杂度一样。

 1 public class Solution {
 2     public int maxSatisfied(int[] customers, int[] grumpy, int x) {
 3         // 前X天happy可以得到的分数
 4         int sum = 0;
 5         for (int i = 0; i < customers.length; i++) {
 6             if (i < x) {
 7                 sum += customers[i];
 8             } else {
 9                 sum += (1 - grumpy[i]) * customers[i];
10             }
11         }
12 
13         int res = sum;
14         for (int i = x; i < customers.length; i++) {
15             // 如果当前生气,但是在窗口内,就可以得分
16             if (grumpy[i] == 1) {
17                 sum += customers[i];
18             }
19             // 如果需要扣分的话,即X天前如果老板生气的话
20             if (grumpy[i - x] == 1) {
21                 sum -= customers[i - x];
22             }
23             res = Math.max(res, sum);
24         }
25         return res;
26     }
27 }

 

相关题目

1052. Grumpy Bookstore Owner

1849. Grumpy Bookstore Owner

LeetCode 题目总结

posted @ 2021-02-24 01:13  CNoodle  阅读(76)  评论(0编辑  收藏  举报