1052. Grumpy Bookstore Owner
两个矩阵
customers:customers[i]代表第i分钟来客数
grumpy:grumpy[i]代表店主在第i分钟心情不好
当店主心情不好,就不能满足客户
店主可以连续minutes保持心情好
请问店主最多可以满足几个客人?
思路:
用sliding window纪录连续minutes中有几个店主心情不好时的来客数
取最多心情不好时的来客数
并且记录所有店主心情好的时候的来客数
最后将这两个值相加就是答案
golang code :
func maxSatisfied(customers []int, grumpy []int, minutes int) int {
n, sum, window, maxcus := len(customers), 0, 0, 0
for i := 0; i < minutes; i++ {
sum += (grumpy[i] ^ 1) * customers[i]
window += grumpy[i] * customers[i]
}
maxcus = window
for i := minutes; i < n; i++ {
sum += (grumpy[i] ^ 1) * customers[i]
window += grumpy[i] * customers[i]
window -= grumpy[i-minutes] * customers[i-minutes]
maxcus = max(maxcus, window)
}
return maxcus + sum
}