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.
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;
}
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 →
Java 8 Solution: