Climbing the Leaderboard

  • + 0 comments

    Simple work around to achieve O(n) time and space complexity taking advantage of the fact that the array is already sorted. Beats the other solutions since we do not have to sort the array again or run binary search n times. Same space complexity as we have to make a new array for the solution anyway.

    def climbingLeaderboard(ranked, player):
        cleanRanked = []
        for score in ranked:
            if len(cleanRanked) > 0 and cleanRanked[-1] == score:
                continue
            cleanRanked.append(score)
            
        res = []
        r = len(cleanRanked) - 1
        for score in player:
            while r >= 0 and score >= cleanRanked[r]:
                r -= 1
            res.append(r+2)
        
        return res