-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcouplesholdinghands.cpp
34 lines (34 loc) · 996 Bytes
/
couplesholdinghands.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
int minSwapsCouples(vector<int>& row) {
map<int,int> index;
int ret=0;
for(int start=0;start<row.size();start++)
index[row[start]]=start;
for(int start=0;start<row.size();start+=2)
{
if(row[start]%2==0)
if(row[start+1]==row[start]+1)
continue;
else
{
index[row[start+1]]=index[row[start]+1];
index[row[start]+1]=start+1;
int temp=row[start+1];
row[start+1]=row[start]+1;
row[index[temp]]=temp;
++ret;
}
else
if(row[start+1]==row[start]-1)
continue;
else
{
index[row[start+1]]=index[row[start]-1];
index[row[start]-1]=start+1;
int temp=row[start+1];
row[start+1]=row[start]-1;
row[index[temp]]=temp;
++ret;
}
}
return ret;
}