Grumpy Bookstore Owner (LeetCode 1052): Solution and Explanation

Medium
Array
Sliding window

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

  1. Compute base, the sum of customers[i] for every minute with grumpy[i] == 0.
  2. Walk i from 0 to n - 1. If minute i is grumpy, add customers[i] to extra.
  3. If i >= minutes and minute i - minutes is grumpy, subtract customers[i - minutes]; that minute just left the window.
  4. Update best = max(best, extra).
  5. 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
  1. Calm minutes 0, 2, 4, 6 give base = 1 + 1 + 1 + 7 = 10.
  2. Window 0..2 rescues the grumpy minute 1 (0 customers): extra = 0.
  3. Window 1..3 rescues minutes 1 and 3: extra = 2, best = 2. Window 2..4 rescues minute 3: extra = 2.
  4. Window 3..5 rescues minutes 3 and 5: extra = 3, best = 3. Window 4..6 rescues minute 5: extra = 1.
  5. Window 5..7 rescues minutes 5 and 7: extra = 6, best = 6.
  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 + best
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

One pass for the base total and one pass for the window.

Space

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 i is i - minutes, not i - minutes + 1.
  • Recomputing the window sum from scratch every step, which turns the solution quadratic when minutes is 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.