• + 0 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.

    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;
    }