1052. Grumpy Bookstore Owner

carlosbf
1 min readJun 21, 2024

--

Daily leetcode challenge for June 21st 2024

Intuition

The problem has the objective to maximize the customers that can be satisfied. There is a remedy to make the store owner not grumpy for m minutes, but these have to be consecutive, so the approach to be used should be sliding window. For non-grumpy minutes just take all of them, and for the minutes he’s grumpy maximize the window of m minutes to get the most customers.

Approach

  • Iterate over n indices and if the owner is not grumpy, just add the customers. Then check if the sliding window covering size m and ending at i maximizes the customers satisfied.
  • Return the sum of customers at non-grumpy minutes plus the maximum window.

Code

const maxSatisfied = (customers, grumpy, minutes) => {

let curr = 0;
let maxim = 0;
let ngm = 0;
for(let i = 0; i < customers.length; i++){
if(grumpy[i] == 1 ){
curr += customers[i];
}else{
ngm += customers[i];
}

if(i >= minutes && grumpy[i-minutes] == 1){
curr -= customers[i-minutes]
}

maxim = Math.max(maxim, curr);
}

return maxim + ngm;
};

Complexity

Time O(n)
Space O(1)

--

--

carlosbf
carlosbf

Written by carlosbf

Software developer that publishes his interview prep and leetcode hobby on this blog

No responses yet