LeetCode solutions

1138. Grumpy Bookstore Owner

My accepted Python solution to LeetCode problem 1138, Grumpy Bookstore Owner, running in 19ms.

  • Difficulty: Medium
  • Python
  • Runtime 19ms
  • Memory 19.9MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 19ms, memory 19.9MB, accepted 2026-01-02.

python
class Solution:
    def maxSatisfied(self, customers: List[int], grumpy: List[int], minutes: int) -> int:
        n = len(customers)
        
        # Base satisfied (when not grumpy)
        base = sum(c for c, g in zip(customers, grumpy) if g == 0)
        
        # Find best window to apply technique
        # Count extra customers we can save in first window
        extra = sum(customers[i] for i in range(minutes) if grumpy[i] == 1)
        max_extra = extra
        
        # Slide the window
        for i in range(minutes, n):
            if grumpy[i] == 1:
                extra += customers[i]
            if grumpy[i - minutes] == 1:
                extra -= customers[i - minutes]
            max_extra = max(max_extra, extra)
        
        return base + max_extra

Source