Skip to content

Instantly share code, notes, and snippets.

@umair-khanzada
Created January 14, 2019 12:15
Show Gist options
  • Save umair-khanzada/250a9958ee4c26811c774e5baf7a1861 to your computer and use it in GitHub Desktop.
Save umair-khanzada/250a9958ee4c26811c774e5baf7a1861 to your computer and use it in GitHub Desktop.
climbingLeaderboard hackerrank
function climbingLeaderboard(scores, alice) {
const uniqueScores = [...new Set(scores)]; //for removing duplicate entries.
let preIndex = uniqueScores.length, //starting position of nested loop.
rank = uniqueScores.length + 1, //last rank default.
aliceRanks = [];
for(let i = 0; i < alice.length; i++){
let s = preIndex;
for(s; s--;){
if(alice[i] < uniqueScores[s]){
aliceRanks.push(rank);
preIndex = s + 1; //update nested loop's starting position, so ignore previously checked elements.
break; //break the loop.
}
else rank -= 1; //Hurry got big score, just update the rank.
}
s < 0 && aliceRanks.push(1); //Hurry no more competitor, got 1st position.
}
return aliceRanks;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment