leetcode1052 爱生气的书店老板

题目

题目

分析

窗口大小固定的滑动窗口问题。

代码

class Solution {
public:
    int maxSatisfied(vector& customers, vector& 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;
    }
};

你可能感兴趣的:(leetcode1052 爱生气的书店老板)