Skip to content

Instantly share code, notes, and snippets.

@wkdalsgh192
Created January 12, 2021 04:05
Show Gist options
  • Save wkdalsgh192/b1190be5282a42cee7fb876cf830515b to your computer and use it in GitHub Desktop.
Save wkdalsgh192/b1190be5282a42cee7fb876cf830515b to your computer and use it in GitHub Desktop.
765. Couples Holding Hands
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