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
  • Apply
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Implementation
  4. Climbing the Leaderboard
  5. Discussions

Climbing the Leaderboard

Problem
Submissions
Leaderboard
Discussions
Editorial

    You are viewing a single comment's thread. Return to all comments →

  • robertgiuga2016
    4 months ago+ 1 comment

    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
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy