In this Climbing the Leaderboard HackerRank solution, An arcade game player wants to climb to the top of the leaderboard and track their ranking. The game uses Dense Ranking, so its leaderboard works like this:
- The player with the highest score is ranked number on the leaderboard.
- Players who have equal scores receive the same ranking number, and the next player(s) receive the immediately following ranking number.
Example
The ranked players will have ranks , , , and , respectively. If the player’s scores are , and , their rankings after each game are , and . Return .
Function Description
Complete the climbingLeaderboard function in the editor below.
climbingLeaderboard has the following parameter(s):
- int ranked[n]: the leaderboard scores
- int player[m]: the player’s scores
Returns
- int[m]: the player’s rank after each new score
Input Format
The first line contains an integer , the number of players on the leaderboard.
The next line contains space-separated integers , the leaderboard scores in decreasing order.
The next line contains an integer, , the number games the player plays.
The last line contains space-separated integers , the game scores.
Constraints
- for
- for
- The existing leaderboard, , is in descending order.
- The player’s scores, , are in ascending order.
Subtask
For of the maximum score:
Sample Input 1
100 100 50 40 40 20 10
4
5 25 50 120
Sample Output 1
4
2
1
Sample Input 2
100 90 90 80 75 60
5
50 65 77 90 102
Sample Output 2
5
4
2
1
Climbing the Leaderboard HackerRank solution
I will Provide solution in Multiple programming languages for you. If you are not able to find the code in required language then please share in comments so that our team can help you.
Problem Solution in C++
vector<int> climbingLeaderboard(vector<int> ranked, vector<int> player)
{
vector<int> res;
auto it = unique(ranked.begin(), ranked.end());
ranked.erase(it, ranked.end());
for(int i = 0; i < player.size(); i++)
{
auto it = lower_bound(ranked.begin(), ranked.end(),player[i], greater<int>());
if(it != ranked.end())
{
res.push_back(distance(ranked.begin(), it) + 1);
}
else
{
res.push_back(ranked.size() + 1);
}
}
return res;
}
Problem Solution in Python
def climbingLeaderboard(ranked, player):
ans=[]
ranked = sorted(list(set(ranked)), reverse = True)
for p in player:
first = 0
last = len(ranked)-1
while first <= last:
found = 0
center = first + int((last - first)/2)
if p < ranked[center]: first = center + 1 elif p > ranked[center]:
last = center - 1
else:
found = 1
ans.append(center + 1)
break
if not found:
if p > ranked[center]:
ans.append(center + 1)
else:
ans.append(center + 2)
return ans
Problem Solution in Java
public static List<Integer> climbingLeaderboard(List<Integer> ranked, List<Integer> player) {
List<Integer> out = new ArrayList<>();
ranked = ranked.stream().distinct().collect(Collectors.toList());
int size = player.size();
int j = 0, i = 0;
while (size > 0 && j < ranked.size()) {
if (player.get(size - 1) >= ranked.get(j)) {
out.add(i++, 1 + j);
size--;
} else {
j++;
}
}
while (size > 0) {
out.add(i++, j+1);
size--;
}
Collections.reverse(out);
return out;
}