-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClimbingTheLeaderboard.cpp
More file actions
42 lines (36 loc) · 1.23 KB
/
Copy pathClimbingTheLeaderboard.cpp
File metadata and controls
42 lines (36 loc) · 1.23 KB
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
/* https://www.hackerrank.com/challenges/climbing-the-leaderboard/problem */
/* Simply replace the given function with my function below.
This solution keeps track of different rankings Alice held as she progressed
through the leaderboard.
*/
vector<int> climbingLeaderboard(vector<int> scores, vector<int> alice) {
// create a set of distinct scores
vector<int> scores_set;
for(int i = 0; i < scores.size(); i++)
{
if(scores[i] == scores[i - 1])
continue;
scores_set.push_back(scores[i]);
}
// create an output vector
vector<int> rank(alice.size());
// define the ending index of the set to start with and move backwards
int index = scores_set.size() - 1;
// for each score of alice, find the index of the corresponding rank
for(int i = 0; i < alice.size(); i++)
{
int j;
for(j = index; j >= 0; j--)
{
if(scores_set[j] > alice[i])
break;
}
if(index == 0 && scores_set[j] < alice[i])
rank[i] = 1;
else
rank[i] = j + 2;
// no need to start from the beginning, simply start at last known score, since it is all ascending
index = j;
}
return rank;
}