Climbing the Leaderboard

Sort by

recency

|

43 Discussions

|

  • + 0 comments

    C++ binary search solution:

    vector climbingLeaderboard(vector ranked, vector player) {

    //erase the ranked duplicates
    ranked.erase(unique(ranked.begin(), ranked.end()), ranked.end());
    
    vector<int> result={};
    for(int i=0; i<player.size(); i++){
        //for each player perform a binary search:
        int left=0,
        right=ranked.size()-1;
        int p = player.at(i);
        bool found = false;
        while(left<=right && !found){
            int mid = left + (right - left) / 2;
            if(ranked.at(mid) == p){
                result.push_back(mid+1);
                found = true;
            }          
            if(ranked.at(mid) > p){
                left = mid + 1;
            }
            else {
                right = mid - 1;
            }
        }
        if(!found){
            result.push_back(left+1);
            ranked.insert(ranked.begin()+left, p);
        }
    }
    return result;
    

    }

  • + 0 comments

    Python 3, no sorting, no extra memory

    def climbingLeaderboard(ranked, player):
        result  = []
        i = 0
        currplace = 1
        pindx = len(player) - 1
        ranked.append(0)
        
        while i < len(ranked):
            if pindx < 0:
                return result
                            
            lastsc = ranked[i]
            
            if player[pindx] >= ranked[i]:
                result .insert(0, currplace)
                pindx -= 1
            else:
                i += 1
                if ranked[i] != lastsc:
                    currplace += 1
          
        return result 
    
  • + 1 comment

    python 3 solution working backwards to find idx

    updated_ranked = sorted(list(set(ranked)), reverse=True)
        result = []
        idx = len(updated_ranked) - 1
        for p in player:
            while True:
                if updated_ranked[idx] > p:
                    result.append(idx+2)
                    break
                elif idx == 0:
                    result.append(1)
                    break
                else:
                    idx = idx - 1
        return result
    
  • + 0 comments

    Python Binary Search Solution

    Passes all test cases

    def binary_search(ranked, score):
        left, right = 0, len(ranked) - 1
        
        if score >= ranked[left]:
            return 1
        elif score < ranked[right]:
            return len(ranked) + 1
        while left < right:
            mid = (left + right) // 2
            
            if score < ranked[mid]:
                left = mid + 1
            elif score > ranked[mid]:
                right = mid
            elif score == ranked[mid]:
                return mid + 1
        mid = (left + right) // 2
        return mid + 1
    
    
    def climbingLeaderboard(ranked, player):
        # Write your code here
        ranked = list(dict.fromkeys(ranked))
        player_ranks = []
        player.sort()
        
        for score in player:
            player_ranks.append(binary_search(ranked, score))
            
        return player_ranks
    
  • + 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;
    

    }