We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

# Climbing the Leaderboard

# Climbing the Leaderboard

+ 0 comments My solution in Python3

My loop logic got a bit dense and though I tried to make this as easy to read as possible, I think it's improvable.

Anybody have any ideas on ways I can improve my approach?

def climbingLeaderboard(ranked, player): # pre-process ranked to remove duplicates rScoresProc = list(dict.fromkeys(ranked)) # a dictionary to keep track of the rank value of each player score playersRanks = {score: 0 for score in player} # create a reversed, sorted, duplicate removed version of player pScoresProc = list(dict.fromkeys(player)) pScoresProc.sort(reverse=True) # traverse through rScoresProc and pScoresProc # and update playersRanks with the rank for each score # index for processed player scores pIdx = 0 # index for processed ranked scores rIdx = 0 # while the scores haven't been exhausted while pIdx < len(pScoresProc): # if a score is placed here, the rank will be 1 more than the rank index rank = rIdx + 1 # the current pScore and rScore given the index currPScore = pScoresProc[pIdx] currRScore = rScoresProc[rIdx] # if the current player score is greater than the current ranked score if currPScore >= currRScore: # update playersRanks playersRanks.update({currPScore: rank}) # move to the next score in pScoresProc if there are still scores if pIdx < len(pScoresProc) - 1: pIdx += 1 continue else: break # if the current player score >= current ranked score else: # increment the rank being considered if possible if rIdx < len(rScoresProc) - 1: rIdx += 1 continue # otherwise, the player score is the lowest possible score else: # update playersRanks playersRanks.update({currPScore: rank + 1}) pIdx += 1 # the final result result = [] # create the result from the dict info given the original player array for score in player: result.append(playersRanks.get(score)) return result

+ 0 comments python3 solution:

def climbingLeaderboard(ranked, player): return_ar = [] max_rank = len(set(ranked)) player_rank = len(player) ranked.reverse() unique_list = list(set(ranked)) unique_list.sort() i = 0 p = 0 p_last_score = -99

`while p < player_rank: item = unique_list[i] p_rank = player[p] if item == unique_list[-1]: if p_rank < unique_list[-1]: return_ar.append(2) else: return_ar.append(1) p += 1 elif p_rank == p_last_score and len(return_ar) > 0: return_ar.append(return_ar[-1]) p += 1 elif p_rank < item: return_ar.append(max_rank + 1) p += 1 elif p_rank == item: return_ar.append(max_rank) p += 1 elif p_rank > item: i += 1 max_rank -= 1 continue p_last_score = p_rank return return_ar`

+ 1 comment Python solution O(n+m)

def climbingLeaderboard(ranked, player): # Write your code here ranked_new = list(dict.fromkeys(ranked)) result = [] N = len(ranked_new) rank = N+1 ind = N-1 for p in player: while ind >= 0: if p >= ranked_new[ind]: rank = ind+1 ind -= 1 else: break result.append(rank) return result

+ 0 comments not passing time limit

function climbingLeaderboard(ranked, player) { let ans = []; let unique = [...new Set(ranked)]; for (let i = 0; i < player.length; i++) { unique.push(player[i]); unique.sort((a,b) => b-a); ans.push(unique.indexOf(player[i]) + 1); } return ans; }

+ 0 comments For those wondering why the example cases timed out, they used cases where it takes to long to process. So for this problem, you are required to use a search method to traverse the array to find the rank.

Load more conversations

Sort 1892 Discussions, By:

Please Login in order to post a comment