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.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Implementation
  4. Climbing the Leaderboard
  5. Discussions

Climbing the Leaderboard

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 1992 Discussions, By:

recency

Please Login in order to post a comment

  • leelinde92
    5 hours ago+ 0 comments

    Here's the implementation in Golang.

    Basically the same as many of the implementations here:

    1. Strip duplicates in memory

    2. 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
     */
    func binarySearchKey(arr []int32, v int32) int {
        i, j := 0, len(arr)
        for i<j {
            h := int(uint(i+j) >> 1)
            if arr[h] > v {
                i = h+1
            } else {
                j = h
            }
        }
        
        return i
    }
    
    func addRank(ranks []int32, scoreIdx int) []int32 {
        return append(ranks, int32(scoreIdx+1))
    }
    
    func stripDuplicates(ranked []int32) []int32 {
        j, l := 0, len(ranked)
        for i:=0;i<l;i++ {               
            if i<l-1 && ranked[i] == ranked[i+1] {
                continue
            }
            
            ranked[j] = ranked[i]
            j++
        }
        
        return ranked[:j]
    }
    
    func climbingLeaderboard(ranked []int32, player []int32) []int32 {
        var ranks []int32 = make([]int32, 0)
        ranked = stripDuplicates(ranked)
        for _, playerScore := range(player) {
            scoreIdx := binarySearchKey(ranked, playerScore)
            ranks = addRank(ranks, scoreIdx)
        }
        
        return ranks
    }
    
    0|
    Permalink
  • redouane_leshaf1
    15 hours ago+ 0 comments

    Here is my code below. when I print the array that must be returned, it contains the desired elements but I couldn't figure out why I'm getting "Wrong Answer" message. Can someone help me find out why?

    int* climbingLeaderboard(int ranked_count, int* ranked, int player_count, int* player, int* result_count) {
        int* positions=malloc(ranked_count*sizeof(int));
        positions[0]=1;
        for(int i=0, j=1; i<ranked_count-1&&j<ranked_count; i++, j++){
            if(ranked[j]<ranked[i]) positions[j]=positions[i]+1;
            else positions[j]=positions[i];
        }
        int* res = malloc(player_count*sizeof(int));
        for(int i=0; i<player_count; i++){
            *(res+i)=positions[ranked_count-1];
            for(int j=ranked_count-1; j>=0; j--){
                if(player[i]>=ranked[j]) *(res+i)=positions[j];
                else if(j==ranked_count-1) *(res+i)=positions[j]+1;
            }
            printf("%d\n", res[i]);
        }
        free(positions);
        return res;
    }
    
    0|
    Permalink
  • ahmed_fitoh_zak1
    1 day ago+ 0 comments

    Here is the code to solve it in Swift First removing the dublicates then using binary search to determine the rank

    func climbingLeaderboard(ranked: [Int], player: [Int]) -> [Int] {
        var result: [Int] = []
        var clearRanked: [Int] = [ranked [0]]
        var total = 1
            for rank in ranked {
                if rank == clearRanked.last! {
                    continue
                } else {
                    clearRanked.append(rank)
                    total += 1
                }
            }
        for playerTrial in player {
            if playerTrial >= clearRanked [0] {
                result.append(1)
            } else if playerTrial == clearRanked.last! {
                result.append(total)
            } else if playerTrial < clearRanked.last! {
                result.append(total + 1)
            } else {
                func findTheRankIndex(arr: [Int], rank: Int, range: Range<Int>) -> Int {
                    if range.lowerBound > range.upperBound {
                        return range.lowerBound
                    } else {
                        let midIndex = range.lowerBound + (range.upperBound - range.lowerBound) / 2
                        if rank == arr [midIndex] {
                            return midIndex + 1
                        } else if rank < arr [midIndex] {
                            if midIndex + 1 < total - 1 &&
                               rank > arr [midIndex + 1] {
                                return midIndex + 2
                            } else {
                                return findTheRankIndex(arr: arr, rank: rank, range: midIndex + 1 ..< range.upperBound)
                            }
                        } else //if rank > arr [midIndex]
                        {
                            if midIndex - 1 > 0 &&
                               rank < arr [midIndex - 1] {
                                return midIndex + 1
                            } else {
                                return findTheRankIndex(arr: arr, rank: rank, range: range.lowerBound ..< midIndex)
                            }
                        }
                    }
                }
                result.append(findTheRankIndex(arr: clearRanked, rank: playerTrial, range: 0 ..< total))
                }
            }
        return result
    }
    
    0|
    Permalink
  • favm72
    3 days ago+ 0 comments
    import bisect
    
    def climbingLeaderboard(ranked, player):   
        ranked=sorted(list(set(ranked)))
        player.sort()
        ladder=[]
        for p in player:        
            i = bisect.bisect_left(ranked, p)
            if i >= len(ranked):
                i = len(ranked) - 1        
            elif p<ranked[i]:
                i-=1            
            ladder.append(len(ranked)-i)       
        return ladder
    
    0|
    Permalink
  • riahiabdelkader1
    4 days ago+ 1 comment

    How can i optimise this more ?

    let uniqueRanked=[... new Set(ranked)].sort((a,b)=>{return b-a});
        let final=[];
        player.map(e=> {
            let i=0;
            while(i<uniqueRanked.length) {
                if(e>=uniqueRanked[i]) {
                    final.push(i+1);
                    break;
                }
                i++;
            }
            i==uniqueRanked.length? final.push(uniqueRanked.length+1):0;
            })
            return final
        }
    
    0|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy