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

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Implementation
  4. Climbing the Leaderboard
  5. Discussions

Climbing the Leaderboard

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 2079 Discussions, By:

recency

Please Login in order to post a comment

  • sidharthan261
    2 days ago+ 1 comment
    def climbingLeaderboard(ranked, player):
        # Write your code here
        rank = []
        for i in range(len(player)):
            ranked.append(player[i])
            transformed = sorted(set(ranked), reverse=True)
            rank.append(transformed.index(player[i]) + 1)
        return rank
    

    Not optimized

    0|
    Permalink
  • robertgiuga2016
    4 days ago+ 0 comments

    Here is a Java 8 solution. Because the test requires better time performance I opted for a binary search. Hope it helps.

    public static List<Integer> climbingLeaderboard(List<Integer> ranked, List<Integer> player) {
            List<Integer> scores= new ArrayList<>(player.size());
            ranked= ranked.stream().distinct().sorted().collect(toList());
            for (Integer p : player) {
                int index = Collections.binarySearch(ranked, p);
                if(index<0){
                    index+=ranked.size()+2;
                }
                else{
                    index= ranked.size()+1 + (-1)*(index+1);
                }
                scores.add(index);
            }
            return scores;
    
        }
    
    0|
    Permalink
  • biengioichantroi
    6 days ago+ 0 comments

    For those using C++, I found this way can pass all the time limited case The idea is same as many of here: find the first one greater than player's score, but using maximum of built-in function to push up the performance:

    vector<int> climbingLeaderboard(vector<int> ranked, vector<int> player) {
        int n = ranked.size(); int m = player.size();
        
        // Xu ly leaderboard
        
        vector<int> stored;
        for (int i=0; i<n; i++) {
            if (i==0) stored.push_back(ranked[i]);
            else {
                if (ranked[i-1]!=ranked[i]) stored.push_back(ranked[i]);
            }
        }
        
        // Xu ly mang player
        vector<int> res; 
        for (int i=0; i<m; i++) {
            int ans = upper_bound(stored.rbegin(),stored.rend(),player[i]) - stored.rbegin();
            int rank = 1+stored.size() - abs(ans);
            res.push_back(rank);
        }
    		
        
        return res;
    }
    
    0|
    Permalink
  • reneroth
    7 days ago+ 0 comments

    Solving this was easy, solving this in the timeout constraints not so much. Took some digging and experimenting in which built in array methods are performant and which not so much - for me, a working solution included using Sets instead of Arrays.

    0|
    Permalink
  • islam_soft_eng
    1 week ago+ 0 comments

    Would someone provide a help optimizing the below code? didn't pass test case 6 and test case 8

    int* climbingLeaderboard(int ranked_count, int* ranked, int player_count, int* player, int* result_count) 
    {
       int j=0;
        int rankNoDups[ranked_count];
        for(int i=0;i<ranked_count;++i)     //loop to remove duplication in ranked array
        {
            if(ranked[i]==ranked[i+1])      //look for similar values.
            {
                continue;                   //skip below lines in case of similar values           
            }
            rankNoDups[j]=ranked[i];        //add unique values to rankNoDups array
            j++;    //advance j by one
        }
    
        int count=1;                          //set count to 1
        *result_count=player_count;       //assign value of player_count to *result_count 
        static int a[100000];              //array to stor 
        for(int k=0;k<player_count;++k)        //5,25,50,120  playerrank
        {    
            for(int i=0;i<j;++i)            //100,50,40,20,10   nodups
            {
                if(rankNoDups[i]<=player[k])
                {
                    break;
                }
               count++;
            }
            *(a+k)=count;
           // printf("%d\n",*(a+k));
            count=1;
        }
       //result_count[k]=count;
       //printf(">>>>>>>%d<<<<<<<<",(sizeof(result_count)/sizeof(result_count[0])));
       return a;
    }
    
    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