← Visualizers
Sliding Window · fixed-size window LeetCode 1052

Grumpy Bookstore Owner / visualized

The reframing is the entire problem, and most people miss it. The answer splits into two independent pieces: banked — every minute where grumpy[i] == 0, satisfied no matter what you do — plus the best rescue, the most grumpy customers a fixed window of minutes width can cover. Compute the banked total once, up front; it is a constant. Then slide a frame of fixed width over the array and maintain one running sum that counts only the grumpy minutes inside it. Two numbers, tracked separately — seeing them as separate is the insight, because the window is not maximising total customers, it is maximising the delta the secret technique buys you.

Execution

idle
customers per minute  ·  grumpy flag beneath
Press Run to begin.
0 / 0
Speed

Java · running line

banked — grumpy 0 rescued by this window entering leaving best rescue window
O(n) time · O(1) space  ·  two passes, two quantities: a constant you bank before the scan, and a fixed-width window that maximises only the grumpy == 1 sum. answer = banked + best.