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.
Climbing the Leaderboard HackerRank Solution in C
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
#define ll long long int
int main(){
ll n,m,i,j,an[200006],am[200005],ar[200005];
scanf("%lld",&n);
for(i=0;i<n;i++)
{
scanf("%lld",an+i);
}
ar[0]=1;
for(i=1;i<n;i++)
{
if(an[i]==an[i-1])
ar[i]=ar[i-1];
else
ar[i]=ar[i-1]+1;
}
ar[n]=ar[n-1]+1;
scanf("%lld",&m);
for(i=0;i<m;i++)
{
scanf("%lld",am+i);
}
i=n-1;j=0;
while(j<m)
{
while(an[i]<am[j] && i>0)
i--;
if(an[i]==am[j])
printf("%lld\n",ar[i]);
else if(an[i]>am[j])
printf("%lld\n",ar[i]+1);
else{
if(ar[i]==1)
printf("1\n");
else
printf("%lld\n",ar[i]-1);}
j++;
}
return 0;
}
Climbing the Leaderboard HackerRank Solution in C++
#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker, "/STACK:67108864")
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <functional>
#include <numeric>
#include <memory.h>
#include <time.h>
using namespace std;
typedef long long LL;
int n, m;
int main()
{
scanf("%d", &n);
set<int> s;
for (int i = 0; i < n; ++i)
{
int x;
scanf("%d", &x);
s.insert(x);
}
scanf("%d", &m);
vector<int> v(s.begin(), s.end());
for (int i = 0; i < m; ++i)
{
int x;
scanf("%d", &x);
int pos = upper_bound(v.begin(), v.end(), x) - v.begin();
printf("%d\n", (int)v.size() - pos + 1);
}
return 0;
}
Climbing the Leaderboard HackerRank Solution in Java
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
List<Integer> scores = new ArrayList<Integer>();
for (int i = 0; i < n; i++){
int score = in.nextInt();
if (scores.size() == 0 || scores.get(scores.size() - 1) != score)
scores.add(score);
}
int m = in.nextInt();
for (int i = 0; i < m; i++){
int score = in.nextInt();
int min = 0;
int max = scores.size();
while (max > min){
int mid = (min + max) / 2;
if (scores.get(mid) <= score)
max = mid;
else
min = mid + 1;
}
System.out.println(min + 1);
}
}
}
Climbing the Leaderboard HackerRank Solution in Python
#!/bin/python
import sys
def compute_sums(A, B):
i = 0
y = A[i]
s = 0
result = {}
for x in B:
while x > y:
result[y] = s
i += 1
if i >= len(A):
return result
y = A[i]
s += 1
for y in A[i:]:
result[y] = s
return result
n = int(raw_input().strip())
A = map(int,raw_input().strip().split(' '))
m = int(raw_input().strip())
B = map(int,raw_input().strip().split(' '))
A = list(set(A))
A.sort()
l = compute_sums(B,A)
for i in B:
print len(A)-l[i]+1
Attempt Climbing the Leaderboard HackerRank Challenge
Link – https://www.hackerrank.com/challenges/climbing-the-leaderboard
Next HackerRank Challenge Solution
Link – https://exploringbits.com/the-hurdle-race-hackerrank-solution/
Aayush Kumar Gupta is the founder and creator of ExploringBits, a website dedicated to providing useful content for people passionate about Engineering and Technology. Aayush has completed his Bachelor of Technology (Computer Science & Engineering) from 2018-2022. From July 2022, Aayush has been working as a full-time Devops Engineer.