2
https://leetcode.com/problems/grumpy-bookstore-owner/7
Looking at the problem, main thing to find is the window where the minutes8
should be used so that the owner is not grumpy and max customers in that9
window can be satisifed.11
Find the window with most no. of satisfied customers given the owner was12
originally grumpy but now has become not grumpy. Since the owner can only be13
not grumpy for minutes duration, this sets our window size as well. So with14
'minutes' window size, find the max window.16
Then add the max window value with the normal satisfied customers.20
int maxSatisfied(vector<int> &customers, vector<int> &grumpy, int minutes) {21
// Tracks the max window where being not grumpy will add most customers and22
// current grumpy window customers23
int max_grumpy_window = 0, grumpy_window = 0;24
int curr = 0, n = customers.size();25
// Total satisifed customers when owner is not grumpy27
// net effective duration of not being grumpy28
minutes = min(minutes, n);30
// process the 1st window31
while (curr < minutes) {32
satisfied_cx += (!grumpy[curr] ? customers[curr] : 0);33
grumpy_window += (grumpy[curr] ? customers[curr] : 0);37
max_grumpy_window = max(max_grumpy_window, grumpy_window);38
// process the remaining windows40
// Remove the 1st element of last window41
grumpy_window -= (grumpy[curr - minutes] ? customers[curr - minutes] : 0);42
// Add the latest element of current window43
grumpy_window += (grumpy[curr] ? customers[curr] : 0);44
max_grumpy_window = max(max_grumpy_window, grumpy_window);46
satisfied_cx += (!grumpy[curr] ? customers[curr] : 0);50
return satisfied_cx + max_grumpy_window;