题目:原题链接(中等)
标签:滑动窗口、数组
解法 | 时间复杂度 | 空间复杂度 | 执行用时 |
Ans 1 (Python) | 144ms (86.27%) | ||
Ans 2 (Python) | |||
Ans 3 (Python) |
解法一:
class Solution:
def minSwaps(self, data: List[int]) -> int:
num = data.count(1)
now = data[:num].count(1)
ans = num - now
for i in range(num, len(data)):
if data[i] == 1:
now += 1
if data[i - num] == 1:
now -= 1
ans = min(ans, num - now)
return