![HackerRank Climbing the Leaderboard Problem Solution](https://thecscience.com/wp-content/uploads/2023/04/HackerRank-Climbing-the-Leaderboard-Problem-Solution.jpg)
HackerRank Climbing the Leaderboard Solution
In this post, We are going to solve HackerRank Climbing the Leaderboard Problem.
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 1 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
ranked [100, 90, 90, 80] =
player (70, 80, 105] =
The ranked players will have ranks 1. 2, 2, and 3. respectively. If the player’s scores are 70. 80 and 105, their rankings after each game is 4th, 3rd, and 1st. Return [4, 3, 1].
Function Description
Complete the climbingLeaderboard function in the editor below.
- int ranked[n]: the leaderboard scores
- int player[m]: the player’s scores
Returns
- intim]: the player’s rank after each new score
Input Format
The first line contains an integer n. the number of players on the leaderboard.
The next line contains n space-separated integers ranked[i], and the leaderboard scores in
decreasing order.
The next line contains an integer, m. the number of games the player plays.
The last line contains m space-separated integers player[j]. the game scores.
Constraints
- 1≤n≤2 x 10
- 1≤m≤2 x 10
- 0 ≤ ranked[i] < 10° for 0 ≤ i ≤ n
- 0≤ player≤ 10″ for 0 ≤ j <m
![HackerRank Climbing the Leaderboard
Problem Solution](https://thecscience.com/wp-content/uploads/2023/04/HackerRank-Climbing-the-Leaderboard-Problem-Solution-1024x576.jpg)
Climbing the Leaderboard C Solution
#include<stdio.h>
struct scoreranks
{
long int score,rank;
};
long int search(struct scoreranks arr[],long int l,long int r,long int no)
{
if(l<=r)
{
long int m=l+(r-l)/2;
if(arr[m].score<=no)
return search(arr,l,m-1,no);
else
return search(arr,m+1,r,no);
}
else
return l;
}
int main()
{
long int n,m,i;
scanf("%ld",&n);
long int scores[n],l;
struct scoreranks scorerank[n];
for(i=0;i<n;i++)scanf("%ld",&scores[i]);
scanf("%ld",&m);
long int alice[m];
for(i=0;i<m;i++)scanf("%ld",&alice[i]);
l=0;
scorerank[l].score=scores[0];
scorerank[l].rank=1;
long int rank=1;
for(i=1;i<n;i++)
{
if(scores[i]!=scores[i-1])
{
rank++;l++;
scorerank[l].score=scores[i];
scorerank[l].rank=rank;
}
}
for(i=0;i<m;i++)
{
long int loc=search(scorerank,0,l,alice[i]);
printf("%ld\n",loc+1);
}
return 0;
}
Climbing the Leaderboard C++ Solution
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
vector<pair<int, int> > vp;
inline int find_rank(int score) {
int lf = 0, rf = vp.size() - 1;
while (lf <= rf) {
int mid = (lf + rf) / 2;
if (vp[mid].first == score) return vp[mid].second;
if (vp[mid].first > score) lf = mid + 1;
else rf = mid - 1;
}
return vp[rf].second + 1;
}
int main() {
int n; cin >> n;
vp.reserve(n);
int last_rank = 0, last_score = 2000000000;
for (int i = 0; i < n; ++i) {
int x; cin >> x;
if (x != last_score) ++last_rank;
last_score = x;
vp.push_back(make_pair(last_score, last_rank));
}
int m; cin >> m;
while (m--) {
int x; cin >> x;
cout << find_rank(x) << '\n';
}
return 0;
}
Climbing the Leaderboard C Sharp Solution
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
static Solution()
{
var inputBuffer = new byte[1048576];
var inputStream = Console.OpenStandardInput(inputBuffer.Length);
Console.SetIn(new StreamReader(inputStream, Console.InputEncoding, false, inputBuffer.Length));
}
static void Main(String[] args) {
var n = Convert.ToInt32(Console.ReadLine());
var scores = Console.ReadLine().Split(' ').Select(x => Convert.ToInt32(x)).Distinct().ToArray();
var m = Convert.ToInt32(Console.ReadLine());
var alice = Console.ReadLine().Split(' ').Select(x => Convert.ToInt32(x)).ToArray();
var aliceScore = 0;
var p = scores.Length-1;
for(var i = 0; i < m; i++)
{
var rank = scores.Length-1;
while (p >= 0 && alice[i] > scores[p]) p--;
if (p == -1)
{
rank = 1;
}
else if (alice[i] == scores[p])
{
rank = p + 1;
}
else if (alice[i] < scores[p])
{
rank = p + 2;
}
Console.WriteLine(rank);
}
}
}
Climbing the Leaderboard Java Solution
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] scores = new int[n];
int[] ranks = new int[n]; //The dense ranking of the scores
//Initialize dense ranking and scores
for(int i=0, rank=1; i < n; i++){
int s = in.nextInt();
scores[i] = s;
if(i > 0 && scores[i-1] != s)
rank++;
ranks[i] = rank;
}
//Interate over Alice's level scores
//int level = 0;
int aliceRank = ranks[ranks.length-1] + 1; //Set it to worst rank+1
int leaderboardIndex = n-1;
int m = in.nextInt();
int prevScore = -1; //Last score we saw
for(int aliceScores=0; aliceScores < m; aliceScores++)
{
int levelScore = in.nextInt();
//We iterate 1 past the front of the array incase we are greater than the best score
for(int i = leaderboardIndex; i >= -1; i--)
{
if(i < 0 || scores[i] > levelScore)
{
System.out.println(aliceRank);
break;
}
else if(scores[i] < levelScore)
{
if(scores[i] != prevScore)//We have went up a ranking
{
aliceRank--;
}
leaderboardIndex--;
}
else//scores[i] == alice[level]
{
leaderboardIndex--;
aliceRank = ranks[i];
}
prevScore = scores[i];
}
}
}
}
Climbing the Leaderboard JavaScript Solution
process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
input_stdin += data;
});
process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});
function readLine() {
return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
function main() {
var n = parseInt(readLine());
scores = readLine().split(' ');
scores = scores.map(Number);
var m = parseInt(readLine());
alice = readLine().split(' ');
alice = alice.map(Number);
const ranks = [];
for (let i = 0; i < n; i++) {
if (!ranks.length || scores[i] < ranks[ranks.length - 1]) ranks.push(scores[i]);
}
console.log(alice.map(a => insertPosition(ranks, a) + 1).join('\n'));
}
function insertPosition(arr, el) {
const n = arr.length;
let low = 0;
let high = n - 1;
let i = -1;
while (low < high) {
const mid = Math.floor((low + high) / 2);
i = mid;
if (el > arr[mid]) {
high = mid;
} else if (el < arr[mid]){
low = mid + 1;
} else {
return i;
}
}
return arr[low] > el ? low + 1 : low;
}
Climbing the Leaderboard Python Solution
#!/bin/python3
import sys
n = int(input().strip())
scores = [int(scores_temp) for scores_temp in input().strip().split(' ')]
m = int(input().strip())
alice = [int(alice_temp) for alice_temp in input().strip().split(' ')]
scores = list(set(scores))
scores.sort()
rank = len(scores)+1
pos = 0
for x in alice:
while pos != len(scores) and x >= scores[pos]:
rank -= 1
pos += 1
print(rank)
Other Solution