Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from typing import List, Union, Collection, Mapping, Optional


class Solution:
def minSwaps(self, data: List[int]) -> int:

# Set window size
k = sum(data)

val = answer = 0

for i, v in enumerate(data):

val += v

if i >= k:
val -= data[i - k]

if i >= k - 1:
answer = max(answer, val)

return k - answer

'''
Window size (k): Count total number of 1s in the array. This is the size of the window needed to fit all 1s.

Sliding window: Move a window of size k across the array and count how many 1s are already in each window position.

Find maximum 1s: Track the maximum number of 1s found in any window (ans).

Calculate swaps: The minimum swaps needed = k - ans (total 1s minus the maximum 1s already grouped in any window).
'''
Loading