Skip to content

Latest commit

 

History

History
33 lines (24 loc) · 610 Bytes

765.md

File metadata and controls

33 lines (24 loc) · 610 Bytes

765 Couples Holding Hands

Description

link


Solution

  • See Code

Code

Complexity T : O(n)

class Solution:
    def minSwapsCouples(self, row: List[int]) -> int:
        d, swap={}, 0
        for i, x in enumerate(row):
            d[x]=i
        for i in range(0, len(row), 2):
            partner=row[i]+1 if row[i]%2==0 else row[i]-1
            j=d[partner]
            if j-i!=1:
                row[i+1], row[j]=row[j], row[i+1]
                d[row[j]]=j
                swap+=1
        return swap