This code solves the problem of maximizing the number of...

June 28, 2025 at 04:58 AM

class Solution { public int maxSatisfied(int[] customers, int[] grumpy, int minutes) { int n = grumpy.length; int i = 0; int curWin = 0; int maxWin = 0; for(int j=0;j<n;j++) { curWin+=customers[j] * grumpy[j]; maxWin = Math.max(curWin, maxWin); if(j >= minutes - 1 ) { curWin -= customers[i] * grumpy[i++]; } } int res = maxWin; for(i=0;i<grumpy.length;i++) { res+=customers[i] * (1 - grumpy[i]); } return res; } }

This code solves the problem of maximizing the number of satisfied customers in a store, where the owner can use a "non-grumpy" technique for a fixed number of consecutive minutes (minutes). Here's what it does step-by-step:

Input:

  • customers: An array where customers[i] represents the number of customers entering the store during the i-th minute.
  • grumpy: An array where grumpy[i] is 1 if the owner is grumpy during the i-th minute (making customers leave dissatisfied) and 0 if not (customers are satisfied).
  • minutes: The maximum duration for which the owner can stop being grumpy continuously.

Goal:

To maximize the number of satisfied customers by strategically using the "non-grumpy" period of length minutes.


Code Explanation:

  1. Variables Initialization:

    • n: Length of the grumpy array (or customers array).
    • curWin: Tracks the extra number of customers that can be satisfied during the current sliding window of minutes where the owner is "non-grumpy".
    • maxWin: The maximum extra customers that can be saved by applying the "non-grumpy" technique.
  2. Sliding Window to Compute Maximum Impact (maxWin):

    • We iterate through each minute (j from 0 to n-1) in the grumpy array.
    • If grumpy[j] is 1, then those otherwise dissatisfied customers (customers[j]) are added to the current window (curWin).
    • Once the window exceeds the allowed minutes size, the oldest value from the window (customers[i] * grumpy[i]) is subtracted, and i is incremented to slide the window forward.
    • Update maxWin with the maximum curWin observed.
  3. Calculate Total Satisfied Customers:

    • Initiate res with maxWin, which represents the maximum extra satisfaction gained during the optimal "non-grumpy" period.
    • Iterate through all the minutes (grumpy.length), adding up customers who are already satisfied when the owner is not grumpy.
  4. Return the Result:

    • The sum in res combines the already satisfied customers and the maximum additional satisfaction from using the "non-grumpy" technique.

Example Walkthrough:

Input:

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

Steps:

  1. Base Satisfaction (without grumpy):

    • Only take satisfied customers where grumpy[i] == 0: customers[0], customers[2], customers[5], and customers[7].
    • Base sum = 1 + 1 + 1 + 5 = 8.
  2. Sliding Window to Maximize Additional Satisfaction:

    • Window size is minutes = 3. Simulate the window:
      • 0-2: Extra = 0 + 0 + 2 = 2.
      • 1-3: Extra = 0 + 2 + 1 = 3.
      • 2-4: Extra = 0 + 1 + 1 = 2.
      • 3-5: Extra = 2 + 1 + 0 = 3.
      • 4-6: Extra = 1 + 0 + 7 = 8. (Max)
      • 5-7: Extra = 0 + 7 + 0 = 7.
    • Max additional satisfaction (maxWin) = 8.
  3. Final Result:

    • Total satisfied customers = Base (8) + Max Additional Satisfaction (8) = 16.

Output:

16

Complexity:

  • Time Complexity: O(n) - A single pass for the sliding window and another pass over the array.
  • Space Complexity: O(1) - In-place computations with no additional data structures.

This code optimally calculates the maximum number of satisfied customers.

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node