Problem :

https://leetcode.com/problems/couples-holding-hands/


My Solution :

class Solution:
def minSwapsCouples(self, row):
ans = 0
pos = {n: i for i, n in enumerate(row)}
for i in range(0, len(row), 2):
left, right = row[i], row[i+1]
if left % 2 == 0:
pair = left + 1
else:
pair = left - 1
if right != pair:
j = pos[pair]
row[i+1], row[j] = row[j], row[i+1]
ans += 1
pos[right] = j
return ans