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 minutei - ๐ค
grumpy[i] = 1means the owner is grumpy at minutei,0means 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
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.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code