Skip to content

Commit 230feeb

Browse files
committed
Runtime: 2744 ms (Top 5.27%) | Memory: 18.7 MB (Top 12.22%)
1 parent c48f444 commit 230feeb

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Runtime: 2744 ms (Top 5.27%) | Memory: 18.7 MB (Top 12.22%)
2+
class Solution:
3+
def minSwaps(self, nums: List[int]) -> int:
4+
width = sum(num == 1 for num in nums) #width of the window
5+
nums += nums
6+
res = width
7+
curr_zeros = sum(num == 0 for num in nums[:width]) #the first window is nums[:width]
8+
9+
for i in range(width, len(nums)):
10+
curr_zeros -= (nums[i - width] == 0) #remove the leftmost 0 if exists
11+
curr_zeros += (nums[i] == 0) #add the rightmost 0 if exists
12+
res = min(res, curr_zeros) #update if needed
13+
14+
return res

0 commit comments

Comments
 (0)