Question:
Today, the bookstore owner has a store open for customers.length minutes. Every minute, some number of customers (customers[i]) enter the store, and all those customers leave after the end of that minute.
On some minutes, the bookstore owner is grumpy. If the bookstore owner is grumpy on the i-th minute, grumpy[i] = 1, otherwise grumpy[i] = 0. When the bookstore owner is grumpy, the customers of that minute are not satisfied, otherwise they are satisfied.
The bookstore owner knows a secret technique to keep themselves not grumpy for X minutes straight, but can only use it once.
Return the maximum number of customers that can be satisfied throughout the day.
Solution:
Maximum number of satisfies customers = total customers - minimum unsatisfied customers
minimum unsatisfied customers = total unsatisfied customers - maximum unsatisfied customers in X time period
So the aim is to find a X time period with maximum unsatisfied customers
Here I move the X lenghth window from the left to the right of the array, each time, calculate the unsatisfied customers, and find the minimum among all the results.
To reduce complexity, use left and right to present the edge of the X length window, each time move from left to right, just add the right one and minus the (left-1) one from the result.
class Solution(object):
def maxSatisfied(self, customers, grumpy, X):
res = 0
for i in range(len(customers)):
res += customers[i]
if len(grumpy) == X:
return res
diff = []
for i in range(len(customers)):
diff.append(customers[i] * grumpy[i])
s =0
for i in range(X,len(customers)):
s += diff[i]
temp = s
left = 1
right = left+X-1
while right < len(customers):
temp = temp + diff[left-1] - diff[right]
if temp < s:
s = temp
left += 1
right += 1
return res - s