Leetcode 1107 Solution
This article provides solution to leetcode question 1107 (minimum-swaps-to-group-all-1s-together)
Access this page by simply typing in "lcs 1107" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/minimum-swaps-to-group-all-1s-together
Solution
class Solution:
def minSwaps(self, data: List[int]) -> int:
size = sum(data)
l = 0
r = 0
cnt = 0
ans = float('inf')
while r < len(data):
if data[r] == 0:
cnt += 1
if r - l + 1 > size:
if data[l] == 0:
cnt -= 1
l += 1
if r - l + 1 == size:
ans = min(ans, cnt)
r += 1
return ans