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):# Remove duplicates and sort scores in descending orderrank_to_score=sorted(set(ranked),reverse=True)# Initialize list to hold rankingsrankings=[]# Get the number of unique ranksrank_num=len(rank_to_score)# Iterate through each player's scoreforscoreinplayer:# Perform binary search to find the correct rankingst,ed=0,rank_num-1mid=(st+ed)// 2whileed-st>1:ifrank_to_score[mid]>score:st=midelifrank_to_score[mid]<score:ed=midelse:st,ed=mid-1,mid# Update mid-pointmid=(st+ed)// 2# Determine ranking based on the search outcome# Case 1: Score is within the rangeifrank_to_score[st]>scoreandscore>=rank_to_score[ed]:rankings.append(ed+1)# Case 2: Score is lower than the lowest score in rankedelifscore<rank_to_score[-1]:rankings.append(rank_num+1)# Case 3: Score is not less than the highest score in rankedelifscore>=rank_to_score[0]:rankings.append(1)else:print('Error:999')returnrankings
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 →