first of all, we can prove that a exchange without any couple emerging is useless.
then we can prove in a couple position like row[6]=11,row[7]=23 , it's identical to fecth 10 or 22.
so this problem turns to be the one that needs you arrange an array of 0~n to it's right index.
you only need to record the exchange num.
class Solution:
def mate(self,i):
return i^1
def minSwapsCouples(self, row):
l=len(row)
loc=[0]*l
done=[False]*l
for i in range(l):
loc[row[i]]=i
ans=0
for i in range(l//2):
j=i*2
while not (row[j]^row[j^1]==1 or done[j]):
ans+=1
m=row[j]^1
l=loc[m]
row[l]=row[j^1]
row[j^1]=m
j=l
# print(ans)
return ans