Maximize the number of satisfied customers by choosing one contiguous time window to suppress grumpiness.
Grumpy Bookstore Owner
gfgProblem
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, allcustomers[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
grumpyvalues. - 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
customersof lengthn. - An integer array
grumpyof lengthn. - An integer
minutesrepresenting 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
grumpy[i]is either0or1
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.