All prompts are owned by LeetCode. To view the prompt, click the title link above.
First completed : August 02, 2024
Last updated : August 02, 2024
Related Topics : Array, Sliding Window
Acceptance Rate : 65.54 %
class Solution:
def minSwaps(self, nums: List[int]) -> int:
totalOnes = nums.count(1)
if not totalOnes :
return 0
oneCount = nums[:totalOnes].count(1)
zeroCount = totalOnes - oneCount
minMoves = zeroCount
for i, nxtVal in enumerate(nums[totalOnes:] + nums[:totalOnes], totalOnes) :
if nxtVal :
oneCount += 1
else :
zeroCount += 1
if nums[i - totalOnes] :
oneCount -= 1
else :
zeroCount -= 1
minMoves = min(minMoves, zeroCount)
return minMoves