Skip to main content
Back to problems
Leetcode
Medium
Arrays
Sliding Window
Greedy
Amazon
1052. Grumpy Bookstore Owner

Maximize the number of satisfied customers by choosing one contiguous time window to suppress grumpiness.

Acceptance 0%
Also Available On
Other platform versions and source mappings for the same problem.

Grumpy Bookstore Owner

gfg
Primary
Problem Statement

Problem

A bookstore owner has a list of customers arriving over n minutes. For each minute i, customers[i] is the number of customers who arrive, and grumpy[i] indicates whether the owner is grumpy during that minute.

  • If grumpy[i] = 0, all customers[i] are satisfied naturally.
  • If grumpy[i] = 1, those customers are unsatisfied unless the owner uses a special technique.

The owner can use the technique for exactly minutes consecutive minutes. During that chosen window, the owner is not grumpy, so all customers in that window become satisfied.

Return the maximum number of satisfied customers possible after choosing the best window.

Notes

  • Customers outside the chosen window are affected only by the original grumpy values.
  • The special technique can be used on any contiguous window of length minutes.
  • You are maximizing the total number of satisfied customers, not minimizing grumpiness.

Input Format

  • An integer array customers of length n.
  • An integer array grumpy of length n.
  • An integer minutes representing the length of the one-time window.

Both arrays have the same length.

Output Format

  • Return a single integer: the maximum number of satisfied customers achievable.

Constraints

  • 1n2×1041 \le n \le 2 \times 10^4
  • 1customers[i]1031 \le customers[i] \le 10^3
  • grumpy[i] is either 0 or 1
  • 1minutesn1 \le minutes \le n
Examples
Sample cases returned by the problem API.

Example 1

Input

customers = [1,1,1,2,1], grumpy = [0,1,0,1,0], minutes = 3

Output

6

Explanation

Without using the technique, the satisfied customers are at minutes 0, 2, and 4: 1 + 1 + 1 = 3. Choosing the window [1, 3] saves the grumpy customers at minutes 1 and 3, adding 1 + 2 = 3 more. Total = 6.

Example 2

Input

customers = [1,0,1,2,1,1,7,5], grumpy = [0,1,0,1,0,1,0,1], minutes = 3

Output

16

Explanation

Baseline satisfied customers come from minutes with grumpy[i] = 0: 1 + 1 + 1 + 7 = 10. The best 3-minute window saves 6 additional customers, so the answer is 16.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.