Grumpy Bookstore Owner
Master this topic with zero to advance depth.
Grumpy Bookstore Owner
A bookstore owner can be grumpy at certain minutes. You can use a 'secret technique' to keep them calm for minutes consecutive minutes. Maximize satisfied customers.
Visual Representation
customers = [1, 0, 1, 2, 1, 1, 7, 5]
grumpy = [0, 1, 0, 1, 0, 1, 0, 1]
minutes = 3
Base satisfaction (owner not grumpy): 1 + 1 + 1 + 7 = 10
Potential extra (using technique on window of size 3):
Win [1, 2, 1] (mins 1-3) -> Extra: 0*c[1] + 1*c[2] + 0*c[3] = 2
Win [7, 5] (mins 6-7) -> ... finding max extra ...Examples
Level I: Brute Force
Intuition
Iterate through every possible starting minute i for the 'X-minute technique'. For each i, calculate the total satisfied customers by summing: (base satisfied customers) + (additional customers satisfied while the technique is active at i).
Thought Process
- Calculate
BaseSatisfied: Sum ofcustomers[j]wheregrumpy[j] == 0for allj. - Iterate through each possible start
ifrom0ton - minutes. - For each
i, calculateAdditional: Sum ofcustomers[j]wheregrumpy[j] == 1andi <= j < i + minutes. - Max Score =
BaseSatisfied + max(Additional).
Detailed Dry Run
| Start | Base | Additional | Total |
|---|---|---|---|
| 0 | 10 | 1+1+0 | 12 |
| 3 | 10 | 2+1+1 | 14 |
| 5 | 10 | 1+5+0 | 16 (MAX) |
Level III: Optimal (Sliding Window)
Intuition
Thought Process
We want to choose a window of size minutes where we 'cancel' the owner's grumpiness to maximize additional customers. This is a classic fixed-size sliding window over the potential 'bonuses'.
Patterns
- Fixed Window: The technique duration is fixed at
minutes. - Additive Bonus: Base satisfied customers are always there; we only focus on maximizing the 'recovered' customers in a window.
Detailed Dry Run
| Window | Recovered | Max Recovered |
|---|---|---|
| [1,0,1] | 0 | 0 |
| [0,1,2] | 3 | 3 |
| [1,2,1] | 3 | 3 |
| [1,1,7] | 8 | 8 (MAX) |
⚠️ Common Pitfalls & Tips
Ensure you only add/remove customers[i] based on grumpy[i] == 1. The base customers grumpy[i] == 0 should not be affected by the sliding window logic after they are counted initially.
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.