-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinPermutations.c
252 lines (221 loc) · 4.65 KB
/
MinPermutations.c
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct queue{
struct node_t *head;
struct node_t *tail;
int count;
};
struct node_t{
int *array;
int level;
int node_number;
int parent;
struct node_t *next;
};
void push_to_q(struct queue *q, struct node_t *node)
{
if (q->head == NULL)
{
q->head = node;
}
else
{
q->tail->next = node;
}
q->tail = node;
q->count += 1;
}
struct node_t* pop_from_head(struct queue *q)
{
struct node_t *node = q->head;
if (q->count == 1)
{
q->head = NULL;
q->tail = NULL;
q->count = 0;
}
else
{
q->head = q->head->next;
q->count -= 1;
}
return node;
}
void reverse_and_copy(int *array, int *dest, int start, int end, int n)
{
for (int i=0; i<start; i++)
{
dest[i] = array[i];
}
for (int i=end+1; i<n; i++)
{
dest[i] = array[i];
}
while (start <= end)
{
dest[start] = array[end];
dest[end--] = array[start++];
}
}
void print_array(int *array, int n)
{
for (int i=0; i<n; i++)
{
printf("%u=%u\n",i,array[i]);
}
printf("\n");
}
bool is_asc(int *array, int n)
{
for (int i=1; i<n; i++)
{
if (array[i] < array[i-1])
{
return false;
}
}
return true;
}
bool arrays_equal(int *a, int *b, int n)
{
for (int i=0; i<n; i++)
{
if (a[i] != b[i])
{
return false;
}
}
return true;
}
bool exists(struct queue *q, struct node_t *node, int n)
{
struct node_t *cur = q->head;
for (int i=0; i<q->count; i++)
{
if (cur == NULL)
{
printf("looking at null node!\n");
return false;
}
if (arrays_equal(cur->array, node->array, n))
{
return true;
}
cur = cur->next;
}
return false;
}
struct node_t* get_parent_node(struct node_t *child, struct queue *q)
{
struct node_t *cur = q->head;
for (int i=0; i<q->count; i++)
{
if (cur == NULL)
{
printf("looking at null node!\n");
return NULL;
}
if (child->parent == cur->node_number)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
void print_array_tree(struct node_t *cur, int n, struct queue *q)
{
while(cur->parent >= 0)
{
print_array(cur->array, n);
cur = get_parent_node(cur, q);
}
}
int minOperations(int arr[], int n) {
struct queue visited = {NULL, NULL, 0};
struct queue to_visit = {NULL, NULL, 0};
int node_number=0;
struct node_t *root = (struct node_t*)malloc(sizeof(struct node_t));
root->array = arr;
root->level = 0;
root->node_number = node_number;
root->parent = -1;
push_to_q(&to_visit, root);
while (to_visit.count != 0)
{
struct node_t *cur = pop_from_head(&to_visit);
push_to_q(&visited, cur);
if (is_asc(cur->array, n))
{
print_array_tree(cur, n, &visited);
print_array(arr, n);
return cur->level;
}
else
{
for (int i=0; i<n; i++)
{
for (int j=i+1; j<n; j++)
{
struct node_t *next = (struct node_t*)malloc(sizeof(struct node_t));
int *array_next = (int*)malloc(n*sizeof(int));
reverse_and_copy(cur->array, array_next, i, j, n);
next->array = array_next;
next->level = cur->level + 1;
if (!exists(&visited, next, n))
{
next->node_number = ++node_number;
next->parent = cur->node_number;
push_to_q(&to_visit, next);
}
else
{
free(array_next);
free(next);
}
}
}
}
}
printf("exiting at to visit q count=%u, visited q count=%u\n", to_visit.count, visited.count);
return 0;
}
// These are the tests we use to determine if the solution is correct.
// You can add your own at the bottom, but they are otherwise not editable!
void printInteger(int n) {
printf("[%d]", n);
}
int test_case_number = 1;
void check(int expected, int output) {
int result = (expected == output);
if (result) {
printf("Pass Test #%d\n", test_case_number);
}
else {
printf("Fail Test #%d: Expected ", test_case_number);
printInteger(expected);
printf(" Your output: ");
printInteger(output);
printf("\n");
}
test_case_number++;
}
int main() {
int n_1 = 5;
int arr_1[] = {1, 2, 5, 4, 3};
int expected_1 = 1;
int output_1 = minOperations(arr_1, n_1);
check(expected_1, output_1);
int n_2 = 3;
int arr_2[] = {3, 1, 2};
int expected_2 = 2;
int output_2 = minOperations(arr_2, n_2);
check(expected_2, output_2);
// Add your own test cases here
int n_3 = 7;
int arr_3[] = {3, 1, 2, 7, 4, 1, 5};
int expected_3 = 4;
int output_3 = minOperations(arr_3, n_3);
check(expected_3, output_3);
}