Grumpy Bookstore Owner (LeetCode 1052): Solution and Explanation
Split the answer into two parts: customers who are satisfied anyway (calm minutes) and customers the one-time trick rescues. The second part is the best sum of grumpy-minute customers over any window of length minutes, found with a fixed sliding window.
Intuition
Where the trick goes has no effect on calm minutes. Those customers are satisfied either way, so add them up once and set them aside.
Inside the trick's window, the only thing that changes is that grumpy minutes become satisfied. So the value of placing the trick at a window is the sum of customers in the grumpy minutes of that window.
Every window has the same length, so moving it one minute to the right adds one minute and removes one. A running sum handles that in O(1) per step.
Approach: Base total + fixed-size sliding window
- Compute
base, the sum ofcustomers[i]for every minute withgrumpy[i] == 0. - Walk
ifrom 0 to n - 1. If minuteiis grumpy, addcustomers[i]toextra. - If
i >= minutesand minutei - minutesis grumpy, subtractcustomers[i - minutes]; that minute just left the window. - Update
best = max(best, extra). - Return
base + best.
Step-by-step example
customers = [1,0,1,2,1,1,7,5], grumpy = [0,1,0,1,0,1,0,1], minutes = 3
- Calm minutes 0, 2, 4, 6 give base = 1 + 1 + 1 + 7 = 10.
- Window 0..2 rescues the grumpy minute 1 (0 customers): extra = 0.
- Window 1..3 rescues minutes 1 and 3: extra = 2, best = 2. Window 2..4 rescues minute 3: extra = 2.
- Window 3..5 rescues minutes 3 and 5: extra = 3, best = 3. Window 4..6 rescues minute 5: extra = 1.
- Window 5..7 rescues minutes 5 and 7: extra = 6, best = 6.
- Answer: 10 + 6 = 16.
Code: Python, Java, C++
1class Solution:
2 def maxSatisfied(self, customers: List[int], grumpy: List[int], minutes: int) -> int:
3 base = 0
4 for c, g in zip(customers, grumpy):
5 if g == 0:
6 base += c
7 extra = best = 0
8 for i in range(len(customers)):
9 if grumpy[i] == 1:
10 extra += customers[i]
11 if i >= minutes and grumpy[i - minutes] == 1:
12 extra -= customers[i - minutes]
13 best = max(best, extra)
14 return base + best1class Solution {
2 public int maxSatisfied(int[] customers, int[] grumpy, int minutes) {
3 int base = 0;
4 for (int i = 0; i < customers.length; i++) {
5 if (grumpy[i] == 0) {
6 base += customers[i];
7 }
8 }
9 int extra = 0, best = 0;
10 for (int i = 0; i < customers.length; i++) {
11 if (grumpy[i] == 1) {
12 extra += customers[i];
13 }
14 if (i >= minutes && grumpy[i - minutes] == 1) {
15 extra -= customers[i - minutes];
16 }
17 best = Math.max(best, extra);
18 }
19 return base + best;
20 }
21}1class Solution {
2public:
3 int maxSatisfied(vector<int>& customers, vector<int>& grumpy, int minutes) {
4 int base = 0;
5 for (int i = 0; i < (int)customers.size(); i++) {
6 if (grumpy[i] == 0) {
7 base += customers[i];
8 }
9 }
10 int extra = 0, best = 0;
11 for (int i = 0; i < (int)customers.size(); i++) {
12 if (grumpy[i] == 1) {
13 extra += customers[i];
14 }
15 if (i >= minutes && grumpy[i - minutes] == 1) {
16 extra -= customers[i - minutes];
17 }
18 best = max(best, extra);
19 }
20 return base + best;
21 }
22};Time and space complexity
O(n)
One pass for the base total and one pass for the window.
O(1)
Only a few running sums.
Other approaches
Try every window directly (time O(n * minutes), space O(1))
For each start, sum the grumpy customers in the next minutes minutes. Easy to write but repeats work across overlapping windows.
Prefix sums of grumpy customers (time O(n), space O(n))
Build a prefix sum of customers[i] * grumpy[i] and read each window's rescued count as a difference of two prefix values. Same O(n) time with an extra array.
Common mistakes
- Adding calm minutes into the window sum. They are already in
base, so they would be counted twice. - Subtracting the outgoing minute even when it was calm, which removes customers that were never added.
- Off-by-one on the window: the minute leaving at step
iisi - minutes, noti - minutes + 1. - Recomputing the window sum from scratch every step, which turns the solution quadratic when
minutesis large.
Frequently asked questions
Why can the calm customers be counted separately?
They are satisfied whether or not the trick covers their minute, so the choice of window cannot change them.
What if minutes equals n?
The only window is the whole day, so every customer is satisfied and the answer is the sum of all customers.
Does it matter which best window is chosen when several tie?
No. The question asks only for the number of satisfied customers, and every tied window gives the same total.
What is the time and space complexity?
O(n) time and O(1) extra space.