forked from chhajed-ajay/Leetcodes-Problem-easy-solution-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.c
More file actions
58 lines (52 loc) · 1009 Bytes
/
Copy pathcode.c
File metadata and controls
58 lines (52 loc) · 1009 Bytes
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<stdio.h>
#include<stdlib.h>
void Union(int H[], int i, int j, int P[])
{
if(H[i] > H[j])
P[j] = i;
else
P[i] = j;
if(H[i] == H[j])
H[i]++;
}
int find(int i, int P[])
{
if(P[i] == -1)
return i;
else{
P[i] = find(P[i], P);
printf("P[%d] : %d\n",i, P[i]);
return P[i];}
}
int minSwapsCouples(int* row, int rowSize){
int j, i, swaps = 0, a, b;
int *H = (int *)malloc(sizeof(int)*rowSize);
int *P = (int *)malloc(sizeof(int)*rowSize);
for(i=0;i<rowSize;i++)
{
H[i] = 0;
P[i] = -1;
}
for(i=0;i<rowSize-1;i+=2)
{
Union(H, row[i], row[i + 1], P);
}
for(i=0;i<rowSize-1;i+=2)
{
a = find(i, P);
b = find(i + 1, P);
// printf("P : %d %d\n",P[i], P[i+1]);
if(a == b)
{
continue;
}
else
{
Union(H, a, b, P);
swaps++;
}
}
return swaps;
free(H);//deleting the allocated memmory which can result into memory leak
free(P);
}