Leetcode 1138 Solution

This article provides solution to leetcode question 1138 (grumpy-bookstore-owner)

https://leetcode.com/problems/grumpy-bookstore-owner

Solution

class Solution:
    def maxSatisfied(self, customers: List[int], grumpy: List[int], X: int) -> int:
        s = sum([
            c for c, g in zip(customers[:X], grumpy[:X]) if g == 1
        ])

        opt_s = s

        l = 0
        r = X - 1

        while r < len(customers) - 1:
            r += 1
            if grumpy[r] == 1:
                s += customers[r]

            if grumpy[l] == 1:
                s -= customers[l]
            l += 1

            opt_s = max(s, opt_s)

        return opt_s + sum([
            c for c, g in zip(customers, grumpy) if g == 0
        ])