Created
January 12, 2021 04:05
-
-
Save wkdalsgh192/b1190be5282a42cee7fb876cf830515b to your computer and use it in GitHub Desktop.
765. Couples Holding Hands
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int minSwapsCouples(int[] row) { | |
int num = 0, temp = 0, cnt=0; | |
for (int i=0;i<row.length;i++) { | |
num=row[i]%2==0?row[i]+1:row[i]-1; | |
if (row[++i] == num) continue; | |
for(int j=i;j<row.length;j++) { | |
if (row[j] == num) { | |
temp = row[i]; | |
row[i]=row[j]; | |
row[j]=temp; | |
cnt++; | |
} | |
} | |
} | |
return cnt; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment