Grumpy Bookstore Owner in Python

The Grumpy Bookstore Owner problem is a classic sliding window optimization challenge. A bookstore owner can use a technique to stay calm for X consecutive minutes, and we need to find the maximum number of happy customers by choosing the optimal window.

Problem Understanding

Given three inputs ?

  • customers[i] − number of customers in minute i
  • grumpy[i] − 1 if owner is grumpy, 0 if calm
  • X − technique duration (consecutive minutes)

When grumpy, customers are unhappy. We can use the technique once to make the owner calm for X minutes. Find the maximum happy customers.

Algorithm Approach

The solution uses a sliding window approach ?

  1. Find the best X-minute window that maximizes additional happy customers
  2. Use the technique on that window (set grumpy to 0)
  3. Count all happy customers

Example

class Solution:
    def maxSatisfied(self, customers, grumpy, X):
        i = 0
        j = 0
        sums = []
        temp = 0
        
        # Build the first window
        while j - i + 1 < X:
            if grumpy[j]:
                temp += customers[j]
            j += 1
        
        sums.append([temp, i, j])
        i += 1
        j += 1
        
        # Slide the window
        while j < len(customers):
            if grumpy[i - 1]:
                temp -= customers[i - 1]
            if grumpy[j]:
                temp += customers[j]
            sums.append([temp, i, j])
            i += 1
            j += 1
        
        # Find the best window
        sums = sorted(sums, key=lambda v: v[0])
        index1 = sums[-1][1]
        index2 = sums[-1][2]
        
        # Apply technique to the best window
        for i in range(index1, index2 + 1):
            grumpy[i] = 0
        
        # Count all happy customers
        ans = 0
        for i in range(len(customers)):
            if not grumpy[i]:
                ans += customers[i]
        
        return ans

# Test the solution
solution = Solution()
customers = [1, 0, 1, 2, 1, 1, 7, 5]
grumpy = [0, 1, 0, 1, 0, 1, 0, 1]
X = 3

result = solution.maxSatisfied(customers, grumpy, X)
print(f"Maximum happy customers: {result}")
Maximum happy customers: 16

How It Works

For the example customers = [1,0,1,2,1,1,7,5] and grumpy = [0,1,0,1,0,1,0,1] with X = 3 ?

  1. Initially happy: customers at minutes 0, 2, 4, 6 = 1 + 1 + 1 + 7 = 10
  2. Best window: minutes 5-7 converts 1 + 5 = 6 unhappy to happy
  3. Total: 10 + 6 = 16 happy customers

Optimized Approach

A more efficient solution without storing all windows ?

def maxSatisfied(customers, grumpy, X):
    # Count already happy customers
    base_happy = sum(customers[i] for i in range(len(customers)) if not grumpy[i])
    
    # Find best window using sliding window
    max_gain = 0
    current_gain = 0
    
    # Calculate gain for first window
    for i in range(X):
        if grumpy[i]:
            current_gain += customers[i]
    max_gain = current_gain
    
    # Slide the window
    for i in range(X, len(customers)):
        if grumpy[i]:
            current_gain += customers[i]
        if grumpy[i - X]:
            current_gain -= customers[i - X]
        max_gain = max(max_gain, current_gain)
    
    return base_happy + max_gain

# Test the optimized solution
customers = [1, 0, 1, 2, 1, 1, 7, 5]
grumpy = [0, 1, 0, 1, 0, 1, 0, 1]
X = 3

result = maxSatisfied(customers, grumpy, X)
print(f"Maximum happy customers: {result}")
Maximum happy customers: 16

Comparison

Approach Time Complexity Space Complexity Best For
Store All Windows O(n log n) O(n) Learning algorithm
Sliding Window O(n) O(1) Optimal performance

Conclusion

Use sliding window technique to find the optimal X-minute interval that maximizes additional happy customers. The optimized approach runs in O(n) time and O(1) space, making it efficient for large inputs.

Updated on: 2026-03-25T08:14:34+05:30

311 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements