|
| 1 | +# [1052. Grumpy Bookstore Owner](https://leetcode.com/problems/grumpy-bookstore-owner) |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +title: "Maximize Satisfied Customers Using Grumpy Bookstore Owner Algorithm" |
| 6 | +summary: "A detailed explanation and implementation of the algorithm to maximize satisfied customers given grumpy periods in a bookstore." |
| 7 | +date: "2024年06月21日" |
| 8 | +modified_date: "2024年06月21日" |
| 9 | +tags: ["algorithm", "sliding window", "JavaScript"] |
| 10 | +slug: "maximize-satisfied-customers-grumpy-bookstore" |
| 11 | + |
| 12 | +--- |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +## Intuition |
| 17 | + |
| 18 | +The initial idea is to identify how we can maximize the number of satisfied customers by considering both the always satisfied customers and those who can be satisfied during grumpy periods with an intervention. |
| 19 | + |
| 20 | +## Approach |
| 21 | + |
| 22 | +We use a sliding window approach to calculate the maximum number of customers that can be satisfied by making the bookstore owner not grumpy for a continuous period of given minutes. The solution involves two main parts: |
| 23 | + |
| 24 | +1. Calculating the number of customers always satisfied when the owner is not grumpy. |
| 25 | +2. Using a sliding window to find the maximum number of extra customers that can be satisfied during grumpy periods within the allowed minutes. |
| 26 | + |
| 27 | +## Complexity |
| 28 | + |
| 29 | +- **Time complexity:** |
| 30 | + $$O(n)$$ where \( n \) is the length of the `customers` array. The sliding window traverses the array once. |
| 31 | + |
| 32 | +- **Space complexity:** |
| 33 | + $$O(1)$$ as we use a constant amount of extra space for the variables. |
| 34 | + |
| 35 | +## Code |
| 36 | + |
| 37 | +```javascript |
| 38 | +/** |
| 39 | + * @param {number[]} customers |
| 40 | + * @param {number[]} grumpy |
| 41 | + * @param {number} minutes |
| 42 | + * @return {number} |
| 43 | + */ |
| 44 | +function maxSatisfied(customers, grumpy, minutes) { |
| 45 | + let alwaysSatisfied = 0; |
| 46 | + let windowStart = 0; |
| 47 | + let extraSatisfied = 0; |
| 48 | + let maxExtraSatisfied = 0; |
| 49 | + |
| 50 | + for (let windowEnd = 0; windowEnd < grumpy.length; windowEnd++) { |
| 51 | + // Add customers to alwaysSatisfied if the owner is not grumpy |
| 52 | + if (grumpy[windowEnd] === 0) { |
| 53 | + alwaysSatisfied += customers[windowEnd]; |
| 54 | + } |
| 55 | + |
| 56 | + // Calculate the number of extra satisfied customers within the current window |
| 57 | + if (grumpy[windowEnd] === 1) { |
| 58 | + extraSatisfied += customers[windowEnd]; |
| 59 | + } |
| 60 | + |
| 61 | + // Once the window exceeds the given minutes, slide the window |
| 62 | + if (windowEnd >= minutes) { |
| 63 | + if (grumpy[windowStart] === 1) { |
| 64 | + extraSatisfied -= customers[windowStart]; |
| 65 | + } |
| 66 | + windowStart++; |
| 67 | + } |
| 68 | + |
| 69 | + // Update the maximum extra satisfied customers |
| 70 | + maxExtraSatisfied = Math.max(maxExtraSatisfied, extraSatisfied); |
| 71 | + } |
| 72 | + |
| 73 | + // The result is the always satisfied customers plus the maximum extra satisfied customers |
| 74 | + return alwaysSatisfied + maxExtraSatisfied; |
| 75 | +} |
| 76 | + |
| 77 | +// Example usage |
| 78 | +console.log( |
| 79 | + maxSatisfied([1, 0, 1, 2, 1, 1, 7, 5], [0, 1, 0, 1, 0, 1, 0, 1], 3) |
| 80 | +); // Output: 16 |
| 81 | +console.log(maxSatisfied([1], [0], 1)); // Output: 1 |
| 82 | +``` |
0 commit comments