Skip to content

Commit 361d1c9

Browse files
committed
Runtime: 209 ms (87.19%), Memory: 18.9 MB (71.55%) - Gitcode
1 parent a8e62cb commit 361d1c9

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from typing import List
2+
3+
class Solution:
4+
def maxSatisfied(self, customers: List[int], grumpy: List[int], minutes: int) -> int:
5+
# Initial satisfied customers count
6+
satisfied_customers = 0
7+
# Additional satisfied customers due to using the technique
8+
additional_satisfied = 0
9+
# Maximum additional satisfied customers we can get
10+
max_additional_satisfied = 0
11+
12+
n = len(customers)
13+
14+
# Calculate the initial satisfied customers and the first window's additional satisfied
15+
for i in range(n):
16+
if grumpy[i] == 0:
17+
satisfied_customers += customers[i]
18+
else:
19+
if i < minutes:
20+
additional_satisfied += customers[i]
21+
22+
# Set max_additional_satisfied to the first window's additional satisfied customers
23+
max_additional_satisfied = additional_satisfied
24+
25+
# Sliding window to find the optimal additional satisfied customers
26+
for i in range(minutes, n):
27+
if grumpy[i] == 1:
28+
additional_satisfied += customers[i]
29+
if grumpy[i - minutes] == 1:
30+
additional_satisfied -= customers[i - minutes]
31+
32+
max_additional_satisfied = max(max_additional_satisfied, additional_satisfied)
33+
34+
# The total maximum satisfied customers is the sum of initially satisfied customers
35+
# and the maximum additional satisfied customers from the optimal window
36+
return satisfied_customers + max_additional_satisfied
37+
38+
# Example usage:
39+
solution = Solution()
40+
print(solution.maxSatisfied([1, 0, 1, 2, 1, 1, 7, 5], [0, 1, 0, 1, 0, 1, 0, 1], 3)) # Output: 16
41+
print(solution.maxSatisfied([1], [0], 1)) # Output: 1

0 commit comments

Comments
 (0)