Skip to main content
LeetCode 1052, Medium. Topics: Array, Sliding Window. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 15 parametrized pytest cases, and a playground notebook:

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

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, full suite in test_solution.py:

Complexity

Tags

NeetCode All.
Last modified on September 7, 2026