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
32
33
|
# 2134. Minimum Swaps to Group All 1's Together II
"""
a swap is defined as taking two distinct position in an array and swapping
the values in them. a circular array is defined as an array where we consider
the first element and the last element to adjacent. given a binary circular
array 'nums', return the minimum number of swaps required to group all 1's
preent in the array together at any location.
"""
class Solution(object):
def minSwaps(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
ones, nums = nums.count(1), nums + nums
x, curr_ones = 0, 0
for i in range(len(nums)):
if i >= ones and nums[i - ones]:
x -= 1
if nums[i] == 1:
x += 1
curr_ones = max(x, curr_ones)
return ones - curr_ones
if __name__ == "__main__":
obj = Solution()
print(obj.minSwaps(nums=[0, 1, 0, 1, 1, 0, 0]))
print(obj.minSwaps(nums=[0, 1, 1, 1, 0, 0, 1, 1, 0]))
print(obj.minSwaps(nums=[1, 1, 0, 0, 1]))
|