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.
Basically the same as many of the implementations here:
Strip duplicates in memory
Binary search
I chose to implement my own function for binary search so that I can understand it better. Go comes with the Search function in the sort package so if you don't want to implement your own you can just use it.
/** * This assumes arr is sorted in descending order */funcbinarySearchKey(arr[]int32,vint32)int{i,j:=0,len(arr)fori<j{h:=int(uint(i+j)>>1)ifarr[h]>v{i=h+1}else{j=h}}returni}funcaddRank(ranks[]int32,scoreIdxint)[]int32{returnappend(ranks,int32(scoreIdx+1))}funcstripDuplicates(ranked[]int32)[]int32{j,l:=0,len(ranked)fori:=0;i<l;i++{ifi<l-1&&ranked[i]==ranked[i+1]{continue}ranked[j]=ranked[i]j++}returnranked[:j]}funcclimbingLeaderboard(ranked[]int32,player[]int32)[]int32{varranks[]int32=make([]int32,0)ranked=stripDuplicates(ranked)for_,playerScore:=range(player){scoreIdx:=binarySearchKey(ranked,playerScore)ranks=addRank(ranks,scoreIdx)}returnranks}
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 →
Here's the implementation in Golang.
Basically the same as many of the implementations here:
Strip duplicates in memory
Binary search
I chose to implement my own function for binary search so that I can understand it better. Go comes with the
Search
function in thesort
package so if you don't want to implement your own you can just use it.