Climbing the Leaderboard

Sort by

recency

|

42 Discussions

|

  • + 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;
    

    }

  • + 0 comments

    Java 8 Solution:

    public static List<Integer> climbingLeaderboard(List<Integer> ranked, List<Integer> player) {
                    // Write your code here
                 //Approach 2: Using Collections.binarySearch() method to find the location.
    
                    // //a. Create the Set of unique Ranked Score from the given list.
                    // Set<Integer> uniqRank = new LinkedHashSet<Integer>();
                    // for(Integer rank : ranked){
                    //     uniqRank.add(rank);
                    // }
                    Set<Integer> uniqRank = new LinkedHashSet<Integer>(ranked);
    
                    // //a.1) Convert the Set to the List collections.
                    List<Integer> uniqRankList = new ArrayList<Integer>(uniqRank);
    
                    // List<Integer> uniqRankList = ranked.stream()
                    //                 .distinct().collect(Collectors.toList());
    
                    //b. Iterate the score of given player and identify the rank. Add to the result List.
                    List<Integer> resultList = new ArrayList<Integer>();
                    for(Integer playerScore : player){
                            int n = Collections.binarySearch(uniqRankList, playerScore, Collections.reverseOrder());
                            // if(uniqRankList.contains(playerScore))resultList.add(n+1);
                            if(n >= 0)resultList.add(n+1);
                            else resultList.add(Math.abs(n));
                    }
    
                    return resultList;
            }