Skip to main content

Climbing the Leaderboard HackerRank Solution in Java with Explanation

Java Solution for Climbing the Leaderboard HackerRank Problem

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.
Learn more about TreeSet and Set Interface :
Guide to Set Interface in Java with SortedSet, HashSet, LinkedHashSet and TreeSet
    • 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. 

Example Explanation :

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 :


Comments

Popular posts from this blog

Queen's Attack II HackerRank Solution in Java with Explanation

Queen's Attack II Problem's Solution in Java (Chessboard Problem)   Problem Description : You will be given a square chess board with one queen and a number of obstacles placed on it. Determine how many squares the queen can attack.  A queen is standing on an n * n chessboard. The chess board's rows are numbered from 1 to n, going from bottom to top. Its columns are numbered from 1 to n, going from left to right. Each square is referenced by a tuple, (r, c), describing the row r and column c, where the square is located. The queen is standing at position (r_q, c_q). In a single move, queen can attack any square in any of the eight directions The queen can move: Horizontally (left, right) Vertically (up, down) Diagonally (four directions: up-left, up-right, down-left, down-right) The queen can move any number of squares in any of these directions, but it cannot move through obstacles. Input Format : n : The size of the chessboard ( n x n ). k : The number of obstacles...

Java Hashset HackerRank Solution | Programming Blog

Java Hashset HackerRank Solution with Explanation   Problem Statement :- In computer science, a set is an abstract data type that can store certain values, without any particular order, and no repeated values. {1,2,3} is an example of a set, but {1,2,2} is not a set. Today you will learn how to use sets in java by solving this problem. You are given n pairs of strings. Two pairs (a,b) and (c,d) are identical if a = c and b = d. That also implies (a,b) is not same as (b,a). After taking each pair as input, you need to print number of unique pairs you currently have. See full problem description in HackerRank Website :- https://www.hackerrank.com/challenges/java-hashset/problem Let's see solution of problem. import java.util.HashSet; import java.util.Scanner; public class Solution {     public static void main(String[] args) {         Scanner s = new Scanner(System.in);         System.out.println("Enter tot...