1052. 爱生气的书店老板 --- 滑动窗口+简单建模
1052. 爱生气的书店老板
题目链接
package com.walegarrett;
/**
* @Author WaleGarrett
* @Date 2020/12/3 21:17
*/
/**
* 1052. 爱生气的书店老板
* 题目链接:https://leetcode-cn.com/problems/grumpy-bookstore-owner/
*/
public class Grumpy_Bookstore_Owner {
public int maxSatisfied(int[] customers, int[] grumpy, int X) {
int len = customers.length;
//保存满意的客户
int[] satisfying = new int[len+1];
satisfying[0] = 0;
for(int i = 1; i <= len; i++){
if(grumpy[i-1] == 0){
satisfying[i] = satisfying[i -1] + customers[i-1];
}else{
satisfying[i] = satisfying[i -1];
}
}
//设置一个窗口
int windowSize = X;
int temp = 0, maxs=Integer.MIN_VALUE;
for(int i=0; i< len; i++){
temp += customers[i];
//窗口已满
if(i >= windowSize){
temp -= customers[i - windowSize];
}
int now = temp;
if(i >= windowSize-1){
int front = i - windowSize + 1;
//加上窗口之前时间满意的客户
now += satisfying[front];
int end = i;
//加上窗口之后时间满意的客户
now += satisfying[len] - satisfying[end + 1];
//System.out.println(now + " "+ satisfying[front] + " " + (satisfying[len] - satisfying[end + 1]));
maxs = Math.max(maxs, now);
}
}
return maxs;
}
}
``
Either Excellent or Rusty