Grumpy Bookstore Owner - Problem

๐Ÿ›๏ธ The Grumpy Bookstore Owner Problem

You're helping optimize customer satisfaction at a local bookstore! The owner runs their shop for n minutes each day, but has a bit of a grumpiness problem.

Here's the situation:

  • ๐Ÿ“Š customers[i] = number of customers entering at minute i
  • ๐Ÿ˜ค grumpy[i] = 1 means the owner is grumpy at minute i, 0 means they're friendly
  • ๐Ÿ˜Š Happy owner = satisfied customers
  • ๐Ÿ˜ก Grumpy owner = unsatisfied customers (they leave upset!)

The secret technique: The owner knows a special meditation method to stay calm for minutes consecutive minutes, but can only use it once per day.

Your goal: Find the maximum number of customers that can be satisfied by strategically timing when to use the meditation technique!

Input & Output

example_1.py โ€” Basic Example
$ Input: customers = [1,0,1,2,1,1,7,5], grumpy = [0,1,0,1,1,1,0,1], minutes = 3
โ€บ Output: 16
๐Ÿ’ก Note: The bookstore owner keeps themselves not grumpy for minutes = 3. Maximum customers = 1 + 1 + 1 + 1 + 7 + 5 = 16. We use technique on minutes [3,4,5] to satisfy customers: 2+1+1=4 additional.
example_2.py โ€” All Grumpy
$ Input: customers = [4,8,2,6], grumpy = [1,1,1,1], minutes = 2
โ€บ Output: 14
๐Ÿ’ก Note: Owner is grumpy all day. Best strategy is to use technique on the 2 consecutive minutes with most customers: positions [0,1] with 4+8=12 customers, or [1,2] with 8+2=10, or [2,3] with 2+6=8. Choose [0,1] for total 14.
example_3.py โ€” Never Grumpy
$ Input: customers = [2,3,4,1], grumpy = [0,0,0,0], minutes = 2
โ€บ Output: 10
๐Ÿ’ก Note: Owner is never grumpy, so all customers are always satisfied regardless of when the technique is used. Total = 2+3+4+1 = 10.

Constraints

  • 1 โ‰ค customers.length โ‰ค 2 ร— 104
  • 0 โ‰ค customers[i] โ‰ค 1000
  • grumpy.length == customers.length
  • grumpy[i] is either 0 or 1
  • 1 โ‰ค minutes โ‰ค customers.length
  • The technique can only be used once during the day

Visualization

Tap to expand
๐Ÿ›๏ธ Bookstore Customer Satisfaction OptimizationTime โ†’3๐Ÿ˜Š+35๐Ÿ˜ก+07๐Ÿ˜ก+02๐Ÿ˜Š+28๐Ÿ˜ก+0๐Ÿง˜ Meditation WindowAfter Applying Technique:3๐Ÿ˜Š+35๐Ÿ˜Š+57๐Ÿ˜Š+72๐Ÿ˜Š+28๐Ÿ˜ก+0๐Ÿ“Š Calculation SummaryBaseline Satisfaction:3 + 2 = 5Technique Window Gain:5 + 7 = 12Total Satisfaction:5 + 12 = 17โœ… Optimal window found at positions [1,2]๐ŸŽฏ Converts 12 unhappy โ†’ happy customers
Understanding the Visualization
1
Identify Baseline
Count customers already satisfied (when owner isn't grumpy)
2
Find Optimal Window
Locate the consecutive time period with most grumpy-minute customers
3
Apply Technique
Use the meditation technique during this optimal window
4
Calculate Total
Add baseline satisfaction + gained satisfaction from technique
Key Takeaway
๐ŸŽฏ Key Insight: The sliding window technique lets us efficiently find the optimal time period to maximize the conversion of unsatisfied customers to satisfied ones, achieving O(n) time complexity instead of O(nร—k) brute force.
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
52.0K Views
Medium Frequency
~15 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen