题目
题目分析
窗口大小固定的滑动窗口问题。
代码
class Solution {
public:
int maxSatisfied(vector<int>& customers, vector<int>& grumpy, int x) {
int res = 0, cur = 0;
for (int i = 0; i < customers.size(); i++){
if (i < x){
cur += customers[i];
}else if (grumpy[i] == 0){
cur += customers[i];
}
}
res = max(res, cur);
for (int i = x; i < customers.size(); i++){
cur -= grumpy[i - x] == 1 ? customers[i - x] : 0;
cur += grumpy[i] == 1 ? customers[i] : 0;
res = max(res, cur);
}
return res;
}
};
网友评论