0
点赞
收藏
分享

微信扫一扫

LeetCode题解(1151):最少交换次数来组合所有的1(Python)


题目:​​原题链接​​(中等)

标签:滑动窗口、数组

解法

时间复杂度

空间复杂度

执行用时

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


举报

相关推荐

0 条评论