Skip to content

Instantly share code, notes, and snippets.

@embedded4ever
Created March 31, 2019 12:53
Show Gist options
  • Save embedded4ever/cb24c8a05a4a8ed70cb83cd0fdb28827 to your computer and use it in GitHub Desktop.
Save embedded4ever/cb24c8a05a4a8ed70cb83cd0fdb28827 to your computer and use it in GitHub Desktop.
Climbing the Leaderboard C Solution
int* climbingLeaderboard(int scores_count, int* scores, int alice_count, int* alice, int* result_count)
{
static unsigned int temp_arraycount=1;
static unsigned int i = 0;
static unsigned int *temp_array;
static unsigned int *rank_array;
temp_array = malloc(sizeof(int) * scores_count);
rank_array = malloc(sizeof(int) * alice_count);
/*memset(temp_array, 0, sizeof(int) * scores_count);
memset(rank_array, 0, sizeof(int) * alice_count);*/
*result_count = alice_count;
for (i = 0; i < scores_count; i++) {
if (i > 0 && *(scores + i) != *(scores + i - 1)) {
temp_arraycount++;
}
temp_array[i] = temp_arraycount; }
for(int a=0; a<alice_count; a++) {
int low = 0;
int top = scores_count-1;
int middle = (low + top) / 2;
rank_array[a] = 1;
while (low <= top) {
middle = (low + top) / 2;
if (*(alice + a) == scores[middle]) {
rank_array[a] = temp_array[middle];
break;
} else if (*(alice + a) < scores[middle]) {
rank_array[a] = temp_array[middle] + 1;
low = middle + 1;
} else {
top = middle - 1;
}
}
}
return rank_array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment