Sort by

recency

|

2350 Discussions

|

  • + 0 comments
      var  poistion_array = []
    
    var poistion_dic = {}
    
    var poistion = 1
    for (var i = 0; i < ranked.length ; i++){
    
        if ( poistion_dic[ranked[i]] == undefined){
    
            poistion_dic[ranked[i]] = poistion
            poistion++
    
        }
    
    }
    
    var previous_index =  ranked.length - 1
    for (var i = 0; i < player.length; i++){
    
        var flag = true
        
    
        for (var j  = previous_index ; j >=0; j--){
              previous_index  = j
            if(player[i] < ranked[j]){
    
                 poistion_array.push(poistion_dic[ranked[j]] +1)
                 j = -1
                 flag = false
    
            }
    
    
            if(player[i] == ranked[j]){
    
                poistion_array.push(poistion_dic[ranked[j]])
                j = -1
                flag = false
    
            }
            
            
        previous_index 
    
    
    
        }
        if (flag ){
    
            poistion_array.push(1)
    
        }
    }
    console.log(poistion_array)
    return poistion_array
    }
    
  • + 0 comments

    var poistion_array = []

    var poistion_dic = {}

    var poistion = 1 for (var i = 0; i < ranked.length ; i++){

    if ( poistion_dic[ranked[i]] == undefined){
    
        poistion_dic[ranked[i]] = poistion
        poistion++
    
    }
    

    }

    var previous_index = ranked.length - 1 for (var i = 0; i < player.length; i++){

    var flag = true
    
    
    for (var j  = previous_index ; j >=0; j--){
          previous_index  = j
        if(player[i] < ranked[j]){
    
             poistion_array.push(poistion_dic[ranked[j]] +1)
             j = -1
             flag = false
    
        }
    
    
        if(player[i] == ranked[j]){
    
            poistion_array.push(poistion_dic[ranked[j]])
            j = -1
            flag = false
    
        }
    
    
    previous_index 
    
    
    
    }
    if (flag ){
    
        poistion_array.push(1)
    
    }
    

    } console.log(poistion_array) return poistion_array }

  • + 0 comments

    Python Method : Using bisect to find the insert point and also is the new rank. However, sorted array must be the ascending because of bisect. Thus, I first delete duplicated scores so that rank number can be corresponding to the len(ranked)-index, and then re-sort it. Next, use bisect_right to find the insert point. When you get the insert point, you need to plus one for correcting rank number (rarher than index value).

    import bisect
    def climbingLeaderboard(ranked, player):
        ranked = sorted(list(set(ranked)))
        new_ranked = []
        for i in player:
            new_ranked.append(len(ranked)-bisect.bisect_right(ranked, i)+1)
        return new_ranked
    
  • + 0 comments

    def climbingLeaderboard(ranked, player):

    unique = []
    
    for i in ranked:
        if not unique or i < unique[-1]:
            unique.append(i)
    
    result = []
    
    ranking = len(unique) -1
    
    for new_score in player:
    
        while new_score > unique[ranking] and ranking >= 0:
            ranking -= 1
    
        # These two cases cover new score less than largest of the ranked score.
        if new_score == unique[ranking]:
            result.append(ranking+1)
    
        if new_score < unique[ranking]:
            result.append(ranking+2)
            unique.insert(ranking+1, new_score)
    
        # This case covers new score higher than largest of the ranked score.
        if ranking == -1:
            result.append(1)
            if new_score > unique[0]:
                unique.insert(0, new_score)
    
    return result
    
  • + 0 comments

    JS/Javascript solution:-

    function climbingLeaderboard(ranked, player) {
        const rankArr = [...(new Set(ranked))];
        const result = [];
        for (let i =0; i< player.length;i++) {
            const playerScore = player[i];
            const last = rankArr.at(-1);
            if (last > playerScore) {
                result.push(rankArr.length + 1);
                continue;
            }
            for (let j = 0; j< rankArr.length;j++) {
                const playerRank = rankArr[j];
                if (playerRank <= playerScore) {
                    result.push(j + 1);
                    break;
                }
            }
        }
        return result;
    }