1052. 爱生气的书店老板

爱生气的书店老板

解法一: 滑动窗口

go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
func maxSatisfied(customers []int, grumpy []int, minutes int) int {
n := len(customers)
sum := 0
saveCustomer := 0
for i := 0; i < minutes; i++ {
if grumpy[i] == 1 {
saveCustomer += customers[i]
} else {
sum += customers[i]
}
}
ans := saveCustomer
for i := minutes; i < n; i++ {
if grumpy[i-minutes] == 1 {
saveCustomer -= customers[i-minutes]
}
if grumpy[i] == 1 {
saveCustomer += customers[i]
} else {
sum += customers[i]
}
ans = max(ans, saveCustomer)
}
return ans + sum
}
作者

wuhunyu

发布于

2024-04-23

更新于

2025-01-15

许可协议