LeetCode 1052. Grumpy Bookstore Owner

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public:
int maxSatisfied(vector<int>& customers, vector<int>& grumpy, int X) {
int n = customers.size(), total = 0, increase = 0, maxIncrease;
for (int i = 0; i < n; ++i)
if (grumpy[i] == 0)
total += customers[i];
for (int i = 0; i < X; ++i)
increase += customers[i] * grumpy[i];
maxIncrease = increase;
for (int i = X; i < n; ++i) {
increase = increase - customers[i - X] * grumpy[i - X] + customers[i] * grumpy[i];
maxIncrease = max(maxIncrease, increase);
}
return total + maxIncrease;
}
};