Original link: https://leetcode.com/problems/grumpy-bookstore-owner/
First, count customers when the owner is impossible to be grumpy
Then use a rolling sum to calculate the maximum number of customers would change their attitude.
Code:
class Solution(object):
def maxSatisfied(self, customers, grumpy, X):
"""
:type customers: List[int]
:type grumpy: List[int]
:type X: int
:rtype: int
"""
op = 0
for i in range(len(customers)):
if grumpy[i] == 0:
op += customers[i]
else:
grumpy[i] = customers[i]
s = 0
for i in range(X):
s += grumpy[i]
maxs = s
for i in range(X, len(customers)):
s += grumpy[i]
s -= grumpy[i-X]
maxs = max(maxs, s)
op += maxs
return op