Leetcode 1052.怒っている書店の所有者(スライディングウィンドウは書かれた質問に偏っています)

 

上司を怒りから怒りに変えられるのは一定の間隔だけであり、元の無生物状態には影響しません。

したがって、上司が怒っている間隔で最大数の顧客を見つけるだけで済みます。これは、スライディングウィンドウ手法を使用して行うことができます。

class Solution {
public:
    int maxSatisfied(vector<int>& customers, vector<int>& grumpy, int X) {
        // 维护一个大小为X的滑动窗口,使得原来改到生气的顾客数量最大
        int n = customers.size();
        int maxScore = 0, score = 0, res = 0;
        for(int i=0;i<n;i++){
            if(grumpy[i]==0){
                res += customers[i];
            }
        }
        for(int i=0,j=0;j<n;j++){
            if(j-i+1>X){
                if(grumpy[i]==1){
                    score-=customers[i];
                }
                i++;
            }
            if(grumpy[j]==1){
                score+=customers[j];
            }
            if(j-i+1==X){
                maxScore = max(maxScore,score);
            }
        }
        return (res+maxScore);
    }
};

 

おすすめ

転載: blog.csdn.net/wwxy1995/article/details/113990399