We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
My TypeScript. Probably could still be optimized, but I took an approach that takes advantage of the player array being sorted. i.e. As you iterate, the next score will either be the same or higher, so there is no need to go back through the whole ranked set each time to find the new ranking.
function climbingLeaderboard(ranked: number[], player: number[]): number[] {
const board: number[] = [...new Set(ranked)];
const res: number[] = [];
let n: number = board.length;
player.forEach((v) => {
while (v > board[n-1]) {
if (n === 1) {
res.push(1);
return;
}
n--;
}
res.push(v === board[n-1] ? n : n + 1);
});
return res;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Climbing the Leaderboard
You are viewing a single comment's thread. Return to all comments →
My TypeScript. Probably could still be optimized, but I took an approach that takes advantage of the player array being sorted. i.e. As you iterate, the next score will either be the same or higher, so there is no need to go back through the whole ranked set each time to find the new ranking.