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.
For Java, using the knowledge that the ranked List is already sorted in descending order:
`public static List climbingLeaderboard(List ranked, List player) {
// Write your code here
final var rankingMap = new TreeMap(Comparator.naturalOrder().reversed());
int currentRank = 1;
for (Integer rank : ranked) {
if (!rankingMap.containsKey(rank)) {
rankingMap.put(rank, currentRank);
currentRank++;
}
}
return player.stream().map(score -> {
final var lowerEntry = rankingMap.lowerEntry(score);
if (lowerEntry == null) {
return 1;
}
return lowerEntry.getValue() + 1;
}).collect(Collectors.toList());
}
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 →
For Java, using the knowledge that the
rankedList is already sorted in descending order:`public static List climbingLeaderboard(List ranked, List player) { // Write your code here final var rankingMap = new TreeMap(Comparator.naturalOrder().reversed()); int currentRank = 1; for (Integer rank : ranked) { if (!rankingMap.containsKey(rank)) { rankingMap.put(rank, currentRank); currentRank++; } }