Climbing the Leaderboard

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