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.
defclimbingLeaderboard(ranked,player):# Time: O(n + m) where n = len(ranked), m = len(player)# Space: O(m) for result storage (excluding input storage)# Initialize result list, ranked index (ri), and current rankresult,ri,rank=[],0,1# Process player scores in reverse order (from highest to lowest)forpiinrange(len(player)-1,-1,-1):# Find the correct position in the ranked leaderboard for current player scorewhileri<len(ranked)andranked[ri]>player[pi]:current_ranked_score=ranked[ri]# Skip all duplicates of the current ranked score# Since ranked is in descending order, duplicates are adjacentwhileri<len(ranked)andcurrent_ranked_score==ranked[ri]:ri+=1# After skipping duplicates, increment rank (new unique score found)rank+=1# At this point, either:# 1. We've exhausted all ranked scores (player score is lowest)# 2. Found first ranked score <= player score (correct rank found)result.append(rank)# Reverse results to match original player score orderreturnresult[::-1]
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Climbing the Leaderboard
You are viewing a single comment's thread. Return to all comments →