标题:1052爱生气的书店老板-简单
题目
有一个书店老板,他的书店开了 n 分钟。每分钟都有一些顾客进入这家商店。给定一个长度为 n 的整数数组 customers ,其中 customers[i] 是在第 i 分钟开始时进入商店的顾客数量,所有这些顾客在第 i 分钟结束后离开。
在某些时候,书店老板会生气。 如果书店老板在第 i 分钟生气,那么 grumpy[i] = 1,否则 grumpy[i] = 0。
当书店老板生气时,那一分钟的顾客就会不满意,若老板不生气则顾客是满意的。
书店老板知道一个秘密技巧,能抑制自己的情绪,可以让自己连续 minutes 分钟不生气,但却只能使用一次。
请你返回 这一天营业下来,最多有多少客户能够感到满意 。
示例1
输入:customers = [1,0,1,2,1,1,7,5], grumpy = [0,1,0,1,0,1,0,1], minutes = 3
输出:16
解释:书店老板在最后 3 分钟保持冷静。
感到满意的最大客户数量 = 1 + 1 + 1 + 1 + 7 + 5 = 16.
示例2
输入:customers = [1], grumpy = [0], minutes = 1
输出:1
提示
- n == customers.length == grumpy.length
- 1 <= minutes <= n <= 2 * 104
- 0 <= customers[i] <= 1000
- grumpy[i] == 0 or 1
代码Go
func maxSatisfied(customers []int, grumpy []int, minutes int) int {
// allScore := 0
oldScore := 0
n := len(customers)
for i := 0; i < n; i++ {
// allScore += customers[i]
if grumpy[i] == 0 {
oldScore += customers[i]
}
}
// 暴力法
// for i := 0; i <= n-minutes; i++ {
// tempScore := 0
// for j := 0; j < minutes; j++ {
// if grumpy[i+j] == 1 {
// tempScore += customers[i+j]
// }
// }
// if tempScore > resScore {
// resScore = tempScore
// }
// }
// 滑动窗口,推出最大优化值
curScore := 0
for i := 0; i < minutes; i++ {
if grumpy[i] == 1 {
curScore += customers[i]
}
}
maxScore := curScore
for i := minutes; i < n; i++ {
if grumpy[i] == 0 && grumpy[i-minutes] == 1 {
curScore -= customers[i-minutes]
} else if grumpy[i] == 1 && grumpy[i-minutes] == 0 {
curScore += customers[i]
} else if grumpy[i] == 1 && grumpy[i-minutes] == 1 {
curScore = curScore + customers[i] - customers[i-minutes]
} else {
continue
}
if curScore > maxScore {
maxScore = curScore
}
}
return maxScore + oldScore
}