Created
January 14, 2019 12:15
-
-
Save umair-khanzada/250a9958ee4c26811c774e5baf7a1861 to your computer and use it in GitHub Desktop.
climbingLeaderboard hackerrank
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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