Climbing the Leaderboard

  • + 0 comments

    JS

    function climbingLeaderboard(ranked, player) {
    const uniqueRanked = Array.from(new Set(ranked));
    const result = [];
    
    let i = uniqueRanked.length - 1;
    
    player.forEach((score) => {
        while (i >= 0 && score >= uniqueRanked[i]) {
            i--;
        }
        // Add 1 because ranks are 1-based, and array indices are 0-based.
        // and 1 for the next rank
        result.push((i + 1) + 1); 
    });
    
    return result;
    

    }