Skip to content

Instantly share code, notes, and snippets.

@ulvimammaadov
Created January 10, 2022 11:26
Show Gist options
  • Save ulvimammaadov/89530e425942b11eda90ec39bf6b5428 to your computer and use it in GitHub Desktop.
Save ulvimammaadov/89530e425942b11eda90ec39bf6b5428 to your computer and use it in GitHub Desktop.
function binarySearch(score, ranked,start = 0,end = ranked.length - 1) {
while (true){
let mid = Math.floor((start + end) / 2);
if (ranked[mid] === score)
return mid + 1;
else if (ranked[mid] > score && ranked[mid + 1] < score)
return mid + 2;
else if (ranked[mid] < score && ranked[mid - 1 > score])
return mid - 1;
if (score < ranked[mid])
start = mid + 1;
else
end = mid - 1;
}
}
function climbingLeaderboard(ranked, player) {
let temp = new Array();
ranked = [...new Set(ranked)];
for (let i = 0; i < player.length; i++) {
if (player[i] >= ranked[0]) {
temp.push(1);
} else if (player[i] < ranked[ranked.length - 1]) {
temp.push(ranked.length + 1);
} else {
temp.push(binarySearch(player[i], ranked));
}
}
return temp;
}
console.log(climbingLeaderboard([100, 90, 90, 80, 75, 60], [50, 65, 77, 90, 102]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment