> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Grumpy Bookstore Owner Python Solution

> Tested Python solution for LeetCode 1052 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 1052, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Sliding Window](/catalog/topics/sliding-window). [View on LeetCode](https://leetcode.com/problems/grumpy-bookstore-owner/description/).

Generate this problem as a practice environment: tested reference solution, 15 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1052   # by problem number
lcpy gen -s grumpy_bookstore_owner   # by problem name
```

## Problem

There is a bookstore owner that has a store open for \<code>n\</code> minutes. You are given an integer array \<code>customers\</code> of length \<code>n\</code> where \<code>customers\[i]\</code> is the number of the customers that enter the store at the start of the \<code>i\<sup>th\</sup>\</code> minute and all those customers leave after the end of that minute.

During certain minutes, the bookstore owner is grumpy. You are given a binary array \<code>grumpy\</code> where \<code>grumpy\[i]\</code> is \<code>1\</code> if the bookstore owner is grumpy during the \<code>i\<sup>th\</sup>\</code> minute, and is \<code>0\</code> otherwise.

When the bookstore owner is grumpy, the customers entering during that minute are not \<strong>satisfied\</strong>. Otherwise, they are satisfied.

The bookstore owner knows a secret technique to remain \<strong>not grumpy\</strong> for \<code>minutes\</code> consecutive minutes, but this technique can only be used \<strong>once\</strong>.

Return the \<strong>maximum\</strong> number of customers that can be \<em>satisfied\</em> throughout the day.

### Examples

```
Input: customers = [1,0,1,2,1,1,7,5], grumpy = [0,1,0,1,0,1,0,1], minutes = 3
Output: 16
Explanation: The bookstore owner keeps themselves not grumpy for the last 3 minutes. The maximum number of customers that can be satisfied = 1 + 1 + 1 + 1 + 7 + 5 = 16.
```

```
Input: customers = [1], grumpy = [0], minutes = 1
Output: 1
```

### Constraints

* \<code>n == customers.length == grumpy.length\</code>
* \<code>1 \<= minutes \<= n \<= 2 \* 10\<sup>4\</sup>\</code>
* \<code>0 \<= customers\[i] \<= 1000\</code>
* \<code>grumpy\[i]\</code> is either \<code>0\</code> or \<code>1\</code>.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/grumpy_bookstore_owner/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/grumpy_bookstore_owner/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n) sliding window
    # Space: O(1)
    def max_satisfied(self, customers: list[int], grumpy: list[int], minutes: int) -> int:
        base = sum(c for c, g in zip(customers, grumpy, strict=True) if g == 0)
        gain = sum(customers[i] * grumpy[i] for i in range(minutes))
        best = gain
        for i in range(minutes, len(customers)):
            gain += customers[i] * grumpy[i] - customers[i - minutes] * grumpy[i - minutes]
            best = max(best, gain)
        return base + best
```

## Complexity

| Time | Space |
| - | - |
| O(n) sliding window | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.