forked from adityabisoi/ds-algo-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.java
45 lines (41 loc) · 1.27 KB
/
solution.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
import java.util.Scanner;
// Time Complexity: O(n + m)
// Space Complexity: O(1) (other than storing input)
public class Solution {
public static void main(String[] args) {
/* Save input - discard duplicates in leaderboard */
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int lastScore = -1;
int[] scores = new int[n];
int index = 0;
while (n-- > 0) {
int currScore = scan.nextInt();
if (currScore != lastScore) {
scores[index] = currScore;
index++;
}
lastScore = currScore;
}
int m = scan.nextInt();
int[] alice = new int[m];
for (int i = 0; i < m; i++) {
alice[i] = scan.nextInt();
}
scan.close();
/* Print ranks */
int i = index - 1;
for (int aliceScore : alice) {
while (i >= 0) {
if (aliceScore < scores[i]) {
System.out.println(i + 2); // add 2 to get correct rank
break;
}
i--;
}
if (i < 0) { // if true, each remaining aliceScore is highest score
System.out.println(1);
}
}
}
}