Java Solution for Climbing the Leaderboard HackerRank Problem
Problem Description :
An arcade game player wants to climb to the top of the leaderboard and track their ranking. The game uses Dense Ranking, so its leaderboard works like this:
- The player with the highest score is ranked number 1 on the leaderboard.
- Players who have equal scores receive the same ranking number, and the next player(s) receive the immediately following ranking number.
Example 1 :
ranked = [100, 90, 90, 80]
player = [70, 80, 105]
Output : [4, 3, 1]
The ranked players will have ranks 1,2,2 and 3, respectively. If the player's scores are 70, 80 and 105, their rankings after each game are 4rth, 3rd and 1st. Return [4, 3, 1].
Example 2 :
ranked = [100, 100, 50, 40, 40, 20, 10]
player = [5, 25, 50, 120]
The ranked players will have ranks 1,1,2,3,3,4,5 respectively. If the
player's scores are 5, 25, 50 and 120, their rankings after each game are 6th, 4rth, 2nd and 1st. Return [6, 4, 2, 1].
Output : [6, 4, 2, 1]
Example 3 :
ranked = [100, 90, 90, 80, 75, 60]
player = [50, 65, 77, 90, 102]
Output : [6, 5, 4, 2, 1]
Here we have ranked player list with decreasing order and player scores list with increase order.
If scores have same number it will count as same ranks. So lets jump on solution.
Solution 1 : Climbing the Leader board Solution in Java
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
public static List<Integer> climbingLeaderboard(List<Integer> ranked, List<Integer> player) {
List<Integer> result = new ArrayList<Integer>();
List<Integer> rankedList = new ArrayList<Integer>();
TreeSet<Integer> set = new TreeSet<Integer>(ranked);
rankedList.addAll(set);
int count = rankedList.size();
outerLoop:
for (int i = 0; i < rankedList.size(); i++) {
int j = 0;
while (rankedList.get(i) > player.get(j)) {
result.add(count + 1);
player.remove(j);
if (player.size() == 0) {
break outerLoop;
}
}
count = count - 1;
}
for (Integer p : player) {
result.add(1);
}
return result;
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int rankedCount = Integer.parseInt(bufferedReader.readLine().trim());
List<Integer> ranked = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
int playerCount = Integer.parseInt(bufferedReader.readLine().trim());
List<Integer> player = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
List<Integer> result = Result.climbingLeaderboard(ranked, player);
bufferedWriter.write(
result.stream()
.map(Object::toString)
.collect(joining("\n"))
+ "\n"
);
bufferedReader.close();
bufferedWriter.close();
}
}
Code Explanation :
- If rank is same it will count as same number, so we will remove duplicates from given ranked list.
- Create new TreeSet class and store all list value to set.
- Now
again create new ArrayList and store Set value to List again. We need
list for getting values based on index. Now we have ranked list with
increasing order value.
- Declare int count variable and initialize with rankedList size.
- Traverse through entire rankedList.
- Add condition in while loop until, ranked list value is greater than player list value.
- Add count in result list and remove 0th index player (We not need now).
- Break for loop if ranked list is empty
- Add 1 to result list if still player list have values.
ranked = [100, 90, 90, 80, 75, 60]
player = [50, 65, 77, 90, 102]
- TreeSet = [60, 75, 80, 90, 100]
- rankedList = [60, 75, 80, 90, 100], count = 5
- i = 0
- j = 0
- 60 > 50 becomes true
- result = [6]
- removed 0th index from player list | player = [65, 77, 90, 102]
- 60 > 65 becomes false
- count = 4
- i = 1
- 75> 65 becomes true
- result = [6, 5]
- player = [77, 90, 100]
- 75 > 77 becomes false
- i = 2
- 80 > 77 becomes true
- result = [6, 5, 4]
- player = [90, 100]
- 80 > 90 becomes false
- i = 3
- 90 > 90 becomes false
- i = 4
- 100 > 90 becomes true
- result = [6, 5, 4, 2]
- 100 > 102 becomes false
- Second for loop
- Still we have 102 left in player list so result = [6, 5, 4, 2, 1]
RECOMMENDED ARTICLE :
- Drawing Book HackerRank Solution in Java and Python with Explanation
- Multi-Dimensional ArrayList in Java | Creating and Store Elements in Nested ArrayList
- Find the Winner of the Circular Game Solution using Recursion | Josephus Problem
- Find Longest Substring Without Repeating Characters | Java and Python
Comments
Post a Comment