class Solution:
def maxSatisfied(self, customers: List[int], grumpy: List[int], X: int) -> int:
size = len(customers)
Sum_pre = 0
#不生气的状态下有多少顾客
for i in range(len(grumpy)):
if grumpy[i]==0:
Sum_pre+=customers[i]
left,right = 0,0
cnt,Max = 0,0
#使用秘密技巧之后,Max是挽回的人数
while right<size:
if grumpy[right]==1:
cnt += customers[right]
right+=1
#这个窗口是不能大于X
while right-left>X:
#大于X的话,就要更新左指针,如果左指针为1,那么就减去左指的值,然后将左指针向右移动
if grumpy[left]==1:
cnt-=customers[left]
left+=1
Max = max(cnt,Max)
#最终结果就是不生气的人数+挽回的人数
return Max+Sum_pre
size = len(customers)
Sum_pre = 0
#不生气的状态下有多少顾客
for i in range(len(grumpy)):
if grumpy[i]==0:
Sum_pre+=customers[i]
#计算第一个窗口内,生气的状态下有技巧可以挽回多少顾客
cur_pre = 0
for i in range(X):
if grumpy[i]==1:
cur_pre+=customers[i]
res_pre = cur_pre
for i in range(X,len(customers)):
if grumpy[i]==1:
cur_pre+=customers[i]
if grumpy[i-X]==1:
cur_pre-=customers[i-X]
res_pre = max(res_pre,cur_pre)
return Sum_pre+res_pre
- 仍然使用滑动窗口的模板来解决这个问题
- 先计算老板不生气的时候有多少顾客
- 再计算老板生气的时候,使用秘密技巧能挽回多少顾客
- 将这两个数字加起来就是最大客户数量了
- 需要维护一个大小为K的窗口
- 更新窗口之中挽回人数的最大值即可