题目有点复杂,简单来说就是求数组指定长度最大和的问题-滑动窗口
class Solution {
public int maxSatisfied(int[] customers, int[] grumpy, int minutes) {
int sum = 0, len = customers.length;
for (int i = 0; i < len; i++) {
// 不生气的部分进行相加,同时把此顾客置0
if (grumpy[i] == 0){
sum += customers[i];
customers[i] = 0;
}
}
int num = customers[0], max = customers[0];
// 滑动窗口
for (int i = 1; i < len; i++){
if (i < minutes) num += customers[i];
else num += customers[i] - customers[i - minutes];
max = Math.max(max, num);
}
return (sum + max);
}
}