[Leetcode] 每日两题 1864 961 -day61

1864. 构成交替字符串需要的最小交换次数

根据贪心的思想,我们可以统计偶数位上1的数量和0的数量,那看哪个更多,这样就移动更少的那个,也就是min(len(s)//2 -res0,len(s)//2 -res1),但是面对序列为奇数个字母时,只能用更少的元素放在偶数位,就不能取min
class Solution:
def minSwaps(self, s: str) -> int:
num0,res0 =0,0
num1,res1 =0,0
for i in range(0,len(s)):
if s[i] =='0':
num0 += 1
if i %2:
res0+=1
else :

[Leetcode] 每日两题 1864 961 -day61最先出现在Python成神之路

版权声明:
作者:zhangchen
链接:https://www.techfm.club/p/14397.html
来源:TechFM
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>