-
Notifications
You must be signed in to change notification settings - Fork 0
/
1331 Rank Transform of an Array.c
284 lines (242 loc) · 6.48 KB
/
1331 Rank Transform of an Array.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
struct Node
{
int val;
int rank;
struct Node * next;
};
struct Node * pos_hashtable[1000000] = {0};
struct Node * neg_hashtable[1000000] = {0};
void merge(int * arr, int low, int mid, int high)
{
int * tmp_arr = (int *)malloc(sizeof(int) * (high - low + 1));
int tmp_arr_index = 0;
int i=0, j=0;
for(i=low, j=mid+1; i<=mid && j<=high; )
{
if(arr[i] <= arr[j])
{
tmp_arr[tmp_arr_index++] = arr[i++];
}
else
{
tmp_arr[tmp_arr_index++] = arr[j++];
}
}
while(i<=mid)
{
tmp_arr[tmp_arr_index++] = arr[i++];
}
while(j<=high)
{
tmp_arr[tmp_arr_index++] = arr[j++];
}
memcpy(&arr[low], tmp_arr, sizeof(int) * tmp_arr_index);
free(tmp_arr);
}
void merge_sort(int * arr, int low, int high)
{
if(low < high)
{
int mid = (low + high)/2;
merge_sort(arr, low, mid);
merge_sort(arr, mid+1, high);
merge(arr, low, mid, high);
}
}
void insert_neg_hashtable(int element, int data)
{
int index = (element * -1);
index %= 1000000;
struct Node * trav_ptr = NULL;
if(neg_hashtable[index] == NULL)
{
struct Node * new_node = (struct Node *)malloc(sizeof(struct Node));
new_node->val = element;
new_node->rank = data;
neg_hashtable[index] = new_node;
}
else
{
struct Node * prev_ptr = NULL;
trav_ptr = neg_hashtable[index];
while(trav_ptr != NULL && trav_ptr->val != element)
{
prev_ptr = trav_ptr;
trav_ptr = trav_ptr->next;
}
if(prev_ptr == NULL)
{
struct Node * new_node = (struct Node *)malloc(sizeof(struct Node));
new_node->val = element;
new_node->rank = data;
neg_hashtable[index] = new_node;
}
else if(trav_ptr == NULL)
{
struct Node * new_node = (struct Node *)malloc(sizeof(struct Node));
new_node->val = element;
new_node->rank = data;
prev_ptr->next = new_node;
}
else if(trav_ptr->val == element)
{
}
}
}
void insert_pos_hashtable(int element, int data)
{
int index = element % 1000000;
struct Node * trav_ptr = NULL;
if(pos_hashtable[index] == NULL)
{
struct Node * new_node = (struct Node *)malloc(sizeof(struct Node));
new_node->val = element;
new_node->rank = data;
pos_hashtable[index] = new_node;
}
else
{
struct Node * prev_ptr = NULL;
trav_ptr = pos_hashtable[index];
while(trav_ptr != NULL && trav_ptr->val != element)
{
prev_ptr = trav_ptr;
trav_ptr = trav_ptr->next;
}
if(prev_ptr == NULL)
{
struct Node * new_node = (struct Node *)malloc(sizeof(struct Node));
new_node->val = element;
new_node->rank = data;
pos_hashtable[index] = new_node;
}
else if(trav_ptr == NULL)
{
struct Node * new_node = (struct Node *)malloc(sizeof(struct Node));
new_node->val = element;
new_node->rank = data;
prev_ptr->next = new_node;
}
else if(trav_ptr->val == element)
{
}
}
}
int search_neg_hashtable(int element)
{
int index = (element * -1);
index %= 1000000;
struct Node * trav_ptr = NULL;
if(neg_hashtable[index]->val == element)
{
return neg_hashtable[index]->rank;
}
else
{
struct Node * prev_ptr = NULL;
trav_ptr = neg_hashtable[index];
while(trav_ptr != NULL && trav_ptr->val != element)
{
prev_ptr = trav_ptr;
trav_ptr = trav_ptr->next;
}
return trav_ptr->rank;
}
}
int search_pos_hashtable(int element)
{
int index = (element % 1000000);
struct Node * trav_ptr = NULL;
if(pos_hashtable[index]->val == element)
{
return pos_hashtable[index]->rank;
}
else
{
struct Node * prev_ptr = NULL;
trav_ptr = pos_hashtable[index];
while(trav_ptr != NULL && trav_ptr->val != element)
{
prev_ptr = trav_ptr;
trav_ptr = trav_ptr->next;
}
return trav_ptr->rank;
}
}
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* arrayRankTransform(int* arr, int arrSize, int* returnSize)
{
int * ret_arr = (int *)malloc(sizeof(int) * arrSize);
int ret_arr_index = 0;
//Sort the arr
int * tmp_arr = (int *)malloc(sizeof(int) * arrSize);
memcpy(tmp_arr, arr, sizeof(int)*arrSize);
merge_sort(tmp_arr, 0, arrSize-1);
//Sorted array is present in tmp_arr, Populate the hashtable
int i=0;
int rank = 1;
int prev_element = INT_MIN;
printf("arrSize : %d\r\n", arrSize);
for(i=0; i<arrSize; i++)
{
if(tmp_arr[i] < 0)
{
if(i==0)
{
insert_neg_hashtable(tmp_arr[i], rank);
prev_element = tmp_arr[i];
rank++;
}
else
{
if(tmp_arr[i] == prev_element)
{
continue;
}
else
{
insert_neg_hashtable(tmp_arr[i], rank);
prev_element = tmp_arr[i];
rank++;
}
}
}
else
{
if(i==0)
{
insert_pos_hashtable(tmp_arr[i], rank);
prev_element = tmp_arr[i];
rank++;
}
else
{
if(tmp_arr[i] == prev_element)
{
continue;
}
else
{
insert_pos_hashtable(tmp_arr[i], rank);
prev_element = tmp_arr[i];
rank++;
}
}
}
}
for(i=0; i<arrSize; i++)
{
if(arr[i] >= 0)
{
ret_arr[ret_arr_index++] = search_pos_hashtable(arr[i]);
}
else
{
ret_arr[ret_arr_index++] = search_neg_hashtable(arr[i]);
}
}
*returnSize = ret_arr_index;
return ret_arr;
}