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...

Sales by Match HackerRank Solution | Java Solution

HackerRank Sales by Match problem solution in Java   Problem Description : Alex works at a clothing store. There is a large pile of socks that must be paired by color for sale. Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are. For example, there are n=7 socks with colors socks = [1,2,1,2,1,3,2]. There is one pair of color 1 and one of color 2 . There are three odd socks left, one of each color. The number of pairs is 2 .   Example 1 : Input : n = 6 arr = [1, 2, 3, 4, 5, 6] Output : 0 Explanation : We have 6 socks with all different colors, So print 0. Example 2 : Input : n = 10 arr = [1, 2, 3, 4, 1, 4, 2, 7, 9, 9] Output : 4 Explanation : We have 10 socks. There is pair of color 1, 2, 4 and 9, So print 4. This problem easily solved by HashMap . Store all pair of socks one by one in Map and check if any pair is present in Map or not. If pair is present then increment ans variable by 1 ...