-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClimbingtheLeaderboard.java
More file actions
92 lines (73 loc) · 2.73 KB
/
Copy pathClimbingtheLeaderboard.java
File metadata and controls
92 lines (73 loc) · 2.73 KB
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
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
import static java.util.stream.IntStream.range;
public class Solution {
static int binarySearch(int[] a, int low, int hi, int key) {
int mid = 0;
while (low <= hi) {
mid = (low + hi) >>> 1;
final int d = a[mid];
if (d == key) {
return mid;
} else if (d > key) {
hi = mid - 1;
} else {
low = ++mid;
}
}
return -mid - 1;
}
// Complete the climbingLeaderboard function below.
static int[] climbingLeaderboard(int[] scores, int[] alice) {
int[] results = new int[alice.length];
int n = scores.length;
int[] distinct = range(0, n).map(i -> scores[n - 1 - i]).distinct().toArray();
int index = 0;
for (int i = 0; i < alice.length; i++) {
int score = alice[i];
index = binarySearch(distinct, index < 0 ? 0 : index, distinct.length - 1, score);
if (index < 0) {
index = -index - 2;
}
results[i] = distinct.length - index;
}
return results;
}
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();
}
}