Alice is playing an arcade game and wants to climb to the top of the leaderboard and wants to track her ranking.
- 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.
For example, the four players on the leaderboard have high scores of , , , and . Those players will have ranks , , , and , respectively. If Alice's scores are , and , her rankings after each game are , and .
Description
It should return an integer array where each element represents Alice's rank after the game.
climbingLeaderboard has the following parameter(s):
- scores: an array of integers that represent leaderboard scores
- alice: an array of integers that represent Alice's scores
Question from hackerrank.
Solution in C# -
class Solution {
static int[] climbingLeaderboard(int[] scores, int[] alice) {
int[] rankings = new int[alice.Length];
List<int> distinct = scores.ToList().Distinct().ToList();
int k = distinct.Count-1;
int rank = distinct.Count+1;
for(int i=0; i<alice.Length; i++)
{
for(int j=k; j>=0; j--)
{
if(alice[i] >= distinct[j])
{
k--;
rank--;
}
else
break;
}
rankings[i] = rank;
}
return rankings;
}
static void Main(string[] args) {
TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
int scoresCount = Convert.ToInt32(Console.ReadLine());
int[] scores = Array.ConvertAll(Console.ReadLine().Split(' '), scoresTemp => Convert.ToInt32(scoresTemp));
int aliceCount = Convert.ToInt32(Console.ReadLine());
int[] alice = Array.ConvertAll(Console.ReadLine().Split(' '), aliceTemp => Convert.ToInt32(aliceTemp));
int[] result = climbingLeaderboard(scores, alice);
textWriter.WriteLine(string.Join("\n", result));
textWriter.Flush();
textWriter.Close();
}
}
Post a Comment