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

Plus Minus HackerRank Solution in Java | Programming Blog

Java Solution for HackerRank Plus Minus Problem Given an array of integers, calculate the ratios of its elements that are positive , negative , and zero . Print the decimal value of each fraction on a new line with 6 places after the decimal. Example 1 : array = [1, 1, 0, -1, -1] There are N = 5 elements, two positive, two negative and one zero. Their ratios are 2/5 = 0.400000, 2/5 = 0.400000 and 1/5 = 0.200000. Results are printed as:  0.400000 0.400000 0.200000 proportion of positive values proportion of negative values proportion of zeros Example 2 : array = [-4, 3, -9, 0, 4, 1]  There are 3 positive numbers, 2 negative numbers, and 1 zero in array. Following is answer : 3/6 = 0.500000 2/6 = 0.333333 1/6 = 0.166667 Lets see solution Solution 1 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.st

Flipping the Matrix HackerRank Solution in Java with Explanation

Java Solution for Flipping the Matrix | Find Highest Sum of Upper-Left Quadrant of Matrix Problem Description : Sean invented a game involving a 2n * 2n matrix where each cell of the matrix contains an integer. He can reverse any of its rows or columns any number of times. The goal of the game is to maximize the sum of the elements in the n *n submatrix located in the upper-left quadrant of the matrix. Given the initial configurations for q matrices, help Sean reverse the rows and columns of each matrix in the best possible way so that the sum of the elements in the matrix's upper-left quadrant is maximal.  Input : matrix = [[1, 2], [3, 4]] Output : 4 Input : matrix = [[112, 42, 83, 119], [56, 125, 56, 49], [15, 78, 101, 43], [62, 98, 114, 108]] Output : 119 + 114 + 56 + 125 = 414 Full Problem Description : Flipping the Matrix Problem Description   Here we can find solution using following pattern, So simply we have to find Max of same number of box like (1,1,1,1). And last