LeetCode Solutions
1052. Grumpy Bookstore Owner
Time: Space:
class Solution {
public:
int maxSatisfied(vector<int>& customers, vector<int>& grumpy, int X) {
int satisfied = 0;
int madeSatisfied = 0;
int windowSatisfied = 0;
for (int i = 0; i < customers.size(); ++i) {
if (grumpy[i] == 0)
satisfied += customers[i];
else
windowSatisfied += customers[i];
if (i >= X && grumpy[i - X] == 1)
windowSatisfied -= customers[i - X];
madeSatisfied = max(madeSatisfied, windowSatisfied);
}
return satisfied + madeSatisfied;
}
};
class Solution {
public int maxSatisfied(int[] customers, int[] grumpy, int X) {
int satisfied = 0;
int madeSatisfied = 0;
int windowSatisfied = 0;
for (int i = 0; i < customers.length; ++i) {
if (grumpy[i] == 0)
satisfied += customers[i];
else
windowSatisfied += customers[i];
if (i >= X && grumpy[i - X] == 1)
windowSatisfied -= customers[i - X];
madeSatisfied = Math.max(madeSatisfied, windowSatisfied);
}
return satisfied + madeSatisfied;
}
}
class Solution:
def maxSatisfied(self, customers: List[int], grumpy: List[int], X: int) -> int:
satisfied = sum(c for i, c in enumerate(customers) if grumpy[i] == 0)
madeSatisfied = 0
windowSatisfied = 0
for i, customer in enumerate(customers):
if grumpy[i] == 1:
windowSatisfied += customer
if i >= X and grumpy[i - X] == 1:
windowSatisfied -= customers[i - X]
madeSatisfied = max(madeSatisfied, windowSatisfied)
return satisfied + madeSatisfied