본문 바로가기
1 Day 1 Algorithms

[2019.02.25] Climbing the Leaderboard

by 곰돌찌 2019. 2. 25.

Problem


Alice is playing an arcade game and wants to climb to the top of the leaderboard and wants to track her ranking. The game uses Dense Ranking, so its leaderboard works like this:

  • The player with the highest score is ranked number  on the leaderboard.
  • Players who have equal scores receive the same ranking number, and the next player(s) receive the immediately following ranking number.

For example, the four players on the leaderboard have high scores of , , , and . Those players will have ranks , , , and , respectively. If Alice's scores are ,  and , her rankings after each game are ,  and .

Function Description

Complete the climbingLeaderboard function in the editor below. It should return an integer array where each element represents Alice's rank after the  game.

climbingLeaderboard has the following parameter(s):

  • scores: an array of integers that represent leaderboard scores
  • alice: an array of integers that represent Alice's scores

Input Format

The first line contains an integer , the number of players on the leaderboard. 
The next line contains  space-separated integers , the leaderboard scores in decreasing order. 
The next line contains an integer, , denoting the number games Alice plays. 
The last line contains  space-separated integers , the game scores.

Constraints

  •  for 
  •  for 
  • The existing leaderboard, , is in descending order.
  • Alice's scores, , are in ascending order.

Subtask

For  of the maximum score:

Output Format

Print  integers. The  integer should indicate Alice's rank after playing the  game.

Sample Input 1

Array: scores1001005040402010 Array: alice52550120
7
100 100 50 40 40 20 10
4
5 25 50 120

Sample Output 1

6
4
2
1

Explanation 1

Alice starts playing with  players already on the leaderboard, which looks like this:

image

After Alice finishes game , her score is  and her ranking is :

image

After Alice finishes game , her score is  and her ranking is :

image

After Alice finishes game , her score is  and her ranking is tied with Caroline at :

image

After Alice finishes game , her score is  and her ranking is :

image

Sample Input 2

Array: scores1009090807560 Array: alice50657790102
6
100 90 90 80 75 60
5
50 65 77 90 102

Sample Output 2

6
5
4
2
1

How I solved the problem


#  중복되는 값들은 제외시켜주기 위해서 HashSet으로 배열을 넣어준 다음에 ArrayList로 변환함

# sorting을 위해 Collections.sort와 reverse함수로 내림차순으로 배열을 정렬해줌

# 반복문을 돌면서 alice가 받은 점수가 중복이 제거된 scoresList의 값보다 작아질 때, 해당 랭크를 배열에 넣어줌!

# 그렇지 않으면 인덱스를 하나씩 줄여줌. 만약 인덱스가 0보다 작아지면 alice의 점수가 모든 scoresList의 값들보다 크므로 1을 넣어줌!!!


# 좀 더 생각해보고 반복문과 배열들을 짤 필요가 있음!!!!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
 
public class Solution {
 
    // Complete the climbingLeaderboard function below.
    static int[] climbingLeaderboard(int[] scores, int[] alice) {
        int scoresLength = scores.length;
        int aliceLength = alice.length;
 
        int[] aliceRank = new int[aliceLength];
        
        HashSet<Integer> scoresSet = new HashSet<Integer>();
 
        for (int i = 0; i < scoresLength; i++) {
            scoresSet.add(scores[i]);
        }
        
        List<Integer> scoresList = new ArrayList<Integer>(scoresSet);
        Collections.sort(scoresList);
        Collections.reverse(scoresList);
 
        int i = scoresList.size() - 1;
        int aliceIndex = 0;
 
        for (int aliceScore : alice) {
            while (i >= 0) {
                if (aliceScore < scoresList.get(i)) {
                    aliceRank[aliceIndex] = i + 2;
                    aliceIndex++;
                    break;
                }
 
                i--;
            }
            if (i < 0) { // if true, each remaining aliceScore is highest score
                aliceRank[aliceIndex] = 1;
                aliceIndex++;
            }
        }       
        
        return aliceRank;
 
    }
 
    private static final Scanner scanner = new Scanner(System.in);
 
    public static void main(String[] args) throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
 
        int scoresCount = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
 
        int[] scores = new int[scoresCount];
 
        String[] scoresItems = scanner.nextLine().split(" ");
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
 
        for (int i = 0; i < scoresCount; i++) {
            int scoresItem = Integer.parseInt(scoresItems[i]);
            scores[i] = scoresItem;
        }
 
        int aliceCount = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
 
        int[] alice = new int[aliceCount];
 
        String[] aliceItems = scanner.nextLine().split(" ");
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
 
        for (int i = 0; i < aliceCount; i++) {
            int aliceItem = Integer.parseInt(aliceItems[i]);
            alice[i] = aliceItem;
        }
 
        int[] result = climbingLeaderboard(scores, alice);
 
        for (int i = 0; i < result.length; i++) {
            bufferedWriter.write(String.valueOf(result[i]));
 
            if (i != result.length - 1) {
                bufferedWriter.write("\n");
            }
        }
 
        bufferedWriter.newLine();
 
        bufferedWriter.close();
 
        scanner.close();
    }
}
 
cs


[출처 : https://www.hackerrank.com ]


'1 Day 1 Algorithms' 카테고리의 다른 글

[2019.02.27] Designer PDF Viewer  (0) 2019.02.27
[2019.02.26] The Hurdle Race  (0) 2019.02.26
[2019.02.22] Picking Numbers  (0) 2019.02.22
[2019.02.20] Forming a Magic Square  (0) 2019.02.20
[2019.02.19] Cats and a Mouse  (0) 2019.02.19

댓글