Climbing the Leaderboard

  • + 0 comments

    Java 8 Solution:

    public static List<Integer> climbingLeaderboard(List<Integer> ranked, List<Integer> player) {
                    // Write your code here
                 //Approach 2: Using Collections.binarySearch() method to find the location.
    
                    // //a. Create the Set of unique Ranked Score from the given list.
                    // Set<Integer> uniqRank = new LinkedHashSet<Integer>();
                    // for(Integer rank : ranked){
                    //     uniqRank.add(rank);
                    // }
                    Set<Integer> uniqRank = new LinkedHashSet<Integer>(ranked);
    
                    // //a.1) Convert the Set to the List collections.
                    List<Integer> uniqRankList = new ArrayList<Integer>(uniqRank);
    
                    // List<Integer> uniqRankList = ranked.stream()
                    //                 .distinct().collect(Collectors.toList());
    
                    //b. Iterate the score of given player and identify the rank. Add to the result List.
                    List<Integer> resultList = new ArrayList<Integer>();
                    for(Integer playerScore : player){
                            int n = Collections.binarySearch(uniqRankList, playerScore, Collections.reverseOrder());
                            // if(uniqRankList.contains(playerScore))resultList.add(n+1);
                            if(n >= 0)resultList.add(n+1);
                            else resultList.add(Math.abs(n));
                    }
    
                    return resultList;
            }