-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClimbingtheLeaderboard.java
53 lines (47 loc) · 1.12 KB
/
ClimbingtheLeaderboard.java
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
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] scores = new int[n];
for (int i = 0; i < scores.length; i++) {
scores[i] = sc.nextInt();
}
int[] ranks = buildRanks(scores);
int m = sc.nextInt();
for (int i = 0; i < m; i++) {
int alice = sc.nextInt();
System.out.println(solve(scores, ranks, alice));
}
sc.close();
}
static int[] buildRanks(int[] scores) {
int[] ranks = new int[scores.length];
int rank = 1;
for (int i = 0; i < ranks.length; i++) {
if (i > 0 && scores[i] != scores[i - 1]) {
rank++;
}
ranks[i] = rank;
}
return ranks;
}
static int solve(int[] scores, int[] ranks, int alice) {
int lower = 0;
int upper = scores.length - 1;
int aliceRank = 1;
while (lower <= upper) {
int middle = (lower + upper) / 2;
if (alice == scores[middle]) {
aliceRank = ranks[middle];
break;
} else if (alice < scores[middle]) {
aliceRank = ranks[middle] + 1;
lower = middle + 1;
} else {
upper = middle - 1;
}
}
return aliceRank;
}
}