Участник:F.Nikitin/MinSwapsCouples

Материал из DISCOPAL
Перейти к: навигация, поиск
class Solution:
    def minSwapsCouples(self, row):
 
        N = len(row)//2
        coups = [[] for i in range(N)]
 
        for seat, num in enumerate(row):
            coups[num//2].append(seat//2)
 
        a = [[] for i in range(N)]
 
        for c1, c2 in coups:
            a[c1].append(c2)
            a[c2].append(c1)
 
        res = 0
 
        for c in range(N):
            if not a[c]: continue
            c1, c2 = c, a[c].pop()
            while c2 != c:
                res += 1
                a[c2].remove(c1)
                c1, c2 = c2, a[c2].pop()
 
        return res