-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsolution.py
More file actions
31 lines (24 loc) · 720 Bytes
/
solution.py
File metadata and controls
31 lines (24 loc) · 720 Bytes
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
26
27
28
29
30
31
def max_subarray_sum_with_k_presses(arr, k, n):
max_length = 0
l = 0
count = {}
index = (0, 0)
for r in range(n):
if arr[r] not in count:
count[arr[r]] = 0
count[arr[r]] += 1
while count[arr[r]] > k:
count[arr[l]] -= 1
l += 1
if r - l + 1 > max_length:
max_length = r - l + 1
index = (max_length, l+1)
return index
def main():
n, k = map(int, input().split())
arr = input()
# find the maximum subarray s.t. every element presses no more than k times
max_subarray_sum = max_subarray_sum_with_k_presses(arr, k, n)
print(*max_subarray_sum)
if __name__ == "__main__":
main()