leetcode1052

2/23/2021 算法滑动窗口

# 解题思路

一次遍历 利用长度为X的滑动窗口,找到用了技能能给自己带来最大收益的滑动窗口的位置

# 代码

class Solution {
    public int maxSatisfied(int[] customers, int[] grumpy, int X) {
        int ans = 0;
        int add = 0;
        int max = 0;
        for (int i = 0; i < customers.length; i++) {
            ans += (1 - grumpy[i]) * customers[i];
            if (i < X) {
                add += grumpy[i]*customers[i];
                max=add;
            }
            else {
                add=add-grumpy[i-X]*customers[i-X]+grumpy[i]*customers[i];
                max=Math.max(max,add);
            }
        }

        return ans + max;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20