File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
Arrays & Strings/#506 - Relative Ranks - Easy Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * Note: The returned array must be malloced, assume caller calls free().
3+ */
4+ typedef struct {
5+ int score ;
6+ int index ;
7+ } ScoreEntry ;
8+
9+ int compareScores (const void * a , const void * b ) {
10+ return ((ScoreEntry * )b )-> score - ((ScoreEntry * )a )-> score ;
11+ }
12+
13+ char * * findRelativeRanks (int * score , int scoreSize , int * returnSize ) {
14+ ScoreEntry * entries = (ScoreEntry * )malloc (sizeof (ScoreEntry ) * scoreSize );
15+ char * * result = (char * * )malloc (sizeof (char * ) * scoreSize );
16+
17+ for (int i = 0 ; i < scoreSize ; i ++ ) {
18+ entries [i ].score = score [i ];
19+ entries [i ].index = i ;
20+ }
21+
22+ qsort (entries , scoreSize , sizeof (ScoreEntry ), compareScores );
23+
24+ for (int i = 0 ; i < scoreSize ; i ++ ) {
25+ int idx = entries [i ].index ;
26+ result [idx ] = (char * )malloc (20 * sizeof (char ));
27+ if (i == 0 ) {
28+ strcpy (result [idx ], "Gold Medal" );
29+ } else if (i == 1 ) {
30+ strcpy (result [idx ], "Silver Medal" );
31+ } else if (i == 2 ) {
32+ strcpy (result [idx ], "Bronze Medal" );
33+ } else {
34+ sprintf (result [idx ], "%d" , i + 1 );
35+ }
36+ }
37+
38+ free (entries );
39+ * returnSize = scoreSize ;
40+ return result ;
41+ }
You can’t perform that action at this time.
0 commit comments