|
| 1 | +--- |
| 2 | +id: grumpy-bookstore-owner |
| 3 | +title: Grumpy Bookstore Owner |
| 4 | +level: medium |
| 5 | +sidebar_label: Grumpy Bookstore Owner |
| 6 | +tags: |
| 7 | + - Array |
| 8 | + - Sliding Window |
| 9 | + - Greedy |
| 10 | + - Java |
| 11 | + - Python |
| 12 | +description: "This document provides solutions for the Grumpy Bookstore Owner problem on LeetCode." |
| 13 | +--- |
| 14 | + |
| 15 | +## Problem Statement |
| 16 | + |
| 17 | +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 customers that enter the store at the start of the `i`th minute and all those customers leave after the end of that minute. |
| 18 | + |
| 19 | +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 `i`th minute, and is `0` otherwise. |
| 20 | + |
| 21 | +When the bookstore owner is grumpy, the customers of that minute are not satisfied, otherwise, they are satisfied. |
| 22 | + |
| 23 | +The bookstore owner knows a secret technique to keep themselves not grumpy for `minutes` consecutive minutes, but can only use it once. |
| 24 | + |
| 25 | +Return the maximum number of customers that can be satisfied throughout the day. |
| 26 | + |
| 27 | +**Example 1:** |
| 28 | + |
| 29 | +Input: `customers = [1,0,1,2,1,1,7,5]`, `grumpy = [0,1,0,1,0,1,0,1]`, `minutes = 3` |
| 30 | +Output: `16` |
| 31 | + |
| 32 | +Explanation: The bookstore owner keeps themselves not grumpy for the last 3 minutes. |
| 33 | +The maximum number of customers that can be satisfied = 1 + 1 + 1 + 1 + 7 + 5 = 16. |
| 34 | + |
| 35 | +**Example 2:** |
| 36 | + |
| 37 | +Input: `customers = [1]`, `grumpy = [0]`, `minutes = 1` |
| 38 | +Output: `1` |
| 39 | + |
| 40 | +**Constraints:** |
| 41 | + |
| 42 | +- `n == customers.length == grumpy.length` |
| 43 | +- `1 <= minutes <= n <= 2 * 10^4` |
| 44 | +- `0 <= customers[i] <= 1000` |
| 45 | +- `grumpy[i]` is either `0` or `1`. |
| 46 | + |
| 47 | +## Solutions |
| 48 | + |
| 49 | +### Intuition |
| 50 | + |
| 51 | +To maximize the number of satisfied customers, we need to: |
| 52 | +1. Calculate the number of satisfied customers when the owner is not grumpy. |
| 53 | +2. Use a sliding window to determine the best time to apply the secret technique to maximize the number of satisfied customers. |
| 54 | + |
| 55 | +### Approach |
| 56 | + |
| 57 | +1. **Initial Satisfaction Calculation:** |
| 58 | + - Calculate the total number of customers satisfied when the owner is not grumpy. |
| 59 | + - Use a variable `max_satisfied` to keep track of this initial number. |
| 60 | + |
| 61 | +2. **Sliding Window Technique:** |
| 62 | + - Use a sliding window of size `minutes` to determine the best period to use the secret technique. |
| 63 | + - Calculate the additional customers satisfied within this window. |
| 64 | + - Update the maximum number of satisfied customers based on this calculation. |
| 65 | + |
| 66 | +### Implementation |
| 67 | + |
| 68 | +The provided code effectively implements the sliding window approach. Here's the breakdown: |
| 69 | + |
| 70 | +1. **Initial Satisfaction Calculation:** |
| 71 | + - Traverse the `customers` array and add up customers where `grumpy[i]` is `0`. |
| 72 | + |
| 73 | +2. **Sliding Window:** |
| 74 | + - Maintain a running sum of customers within the current window where `grumpy[i]` is `1`. |
| 75 | + - Slide the window across the array and update the maximum satisfied customers. |
| 76 | + |
| 77 | +### Java |
| 78 | + |
| 79 | +```java |
| 80 | +class Solution { |
| 81 | + public int maxSatisfied(int[] customers, int[] grumpy, int minutes) { |
| 82 | + int base = 0; |
| 83 | + int currentWindow = 0; |
| 84 | + int len = customers.length; |
| 85 | + for(int i = 0; i < len; i++) { |
| 86 | + if(grumpy[i] == 0) base += customers[i]; |
| 87 | + else if(i < minutes) currentWindow += customers[i]; |
| 88 | + } |
| 89 | + int maxWindow = currentWindow; |
| 90 | + for(int i = minutes; i < len; i++) { |
| 91 | + currentWindow += (customers[i] * grumpy[i]); |
| 92 | + currentWindow -= (customers[i - minutes] * grumpy[i - minutes]); |
| 93 | + maxWindow = Math.max(currentWindow, maxWindow); |
| 94 | + } |
| 95 | + return maxWindow + base; |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +``` |
| 100 | + |
| 101 | +### Python |
| 102 | + |
| 103 | +```Python |
| 104 | +class Solution: |
| 105 | + def maxSatisfied(self, customers, grumpy, minutes): |
| 106 | + base = 0 |
| 107 | + current_window = 0 |
| 108 | + length = len(customers) |
| 109 | + |
| 110 | + for i in range(length): |
| 111 | + if grumpy[i] == 0: |
| 112 | + base += customers[i] |
| 113 | + elif i < minutes: |
| 114 | + current_window += customers[i] |
| 115 | + |
| 116 | + max_window = current_window |
| 117 | + |
| 118 | + for i in range(minutes, length): |
| 119 | + current_window += customers[i] * grumpy[i] |
| 120 | + current_window -= customers[i - minutes] * grumpy[i - minutes] |
| 121 | + max_window = max(current_window, max_window) |
| 122 | + |
| 123 | + return max_window + base |
| 124 | +``` |
| 125 | + |
| 126 | +###conclusion |
| 127 | + |
| 128 | +Complexity |
| 129 | +Time Complexity: $O(n)$ |
| 130 | +The algorithm iterates through the array twice (once for the initial satisfaction calculation and once for the sliding window), resulting in a linear time complexity. |
| 131 | + |
| 132 | +Space Complexity: $O(1)$ |
| 133 | +The algorithm uses a constant amount of extra space for variables like satisfied, additional, and maxAdditional. |
| 134 | + |
0 commit comments