[
array
accumulate
sliding window
]
BinarySearch 1033 Group the Ones Sequel
Problem statement
https://binarysearch.com/problems/Group-the-Ones-Sequel/
Solution
Variation of Leetcode 1151 Minimum Swaps to Group All 1’s Together, just double our array.
Complexity
It is O(n)
for time and space.
Code
class Solution:
def solve(self, data):
data = [int(i) for i in data]
k, n = sum(data), len(data)
acc = [0] + list(accumulate(data*2))
return k - max(acc[i+k] - acc[i] for i in range(2*n - k + 1))