-
Notifications
You must be signed in to change notification settings - Fork 694
/
Copy pathSolution.java
77 lines (68 loc) · 2.52 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
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
//Problem: https://www.hackerrank.com/challenges/climbing-the-leaderboard
//Java 8
/*
Initial Thoughts:
We want to build a dense ranking array based on the scores as
we read them in
Using that and a few pointer variables we can iterate 1 time
over the scores array, advancing an unknown number of steps
for each score that Alice has. For each score Alice has we
will update our leaderboard index, which is our regular
ranking and update our rank which is our dense ranking
Time Complexity: O(n + m) //We only iterate over the scores and alice's scores once
Space Complexity: O(n) //We do not store alice's scores in memory, so our space complexity is just n for storing the scores array
*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] scores = new int[n];
int[] ranks = new int[n]; //The dense ranking of the scores
//Initialize dense ranking and scores
for(int i=0, rank=1; i < n; i++){
int s = in.nextInt();
scores[i] = s;
if(i > 0 && scores[i-1] != s)
rank++;
ranks[i] = rank;
}
//Interate over Alice's level scores
//int level = 0;
int aliceRank = ranks[ranks.length-1] + 1; //Set it to worst rank+1
int leaderboardIndex = n-1;
int m = in.nextInt();
int prevScore = -1; //Last score we saw
for(int aliceScores=0; aliceScores < m; aliceScores++)
{
int levelScore = in.nextInt();
//We iterate 1 past the front of the array incase we are greater than the best score
for(int i = leaderboardIndex; i >= -1; i--)
{
if(i < 0 || scores[i] > levelScore)
{
System.out.println(aliceRank);
break;
}
else if(scores[i] < levelScore)
{
if(scores[i] != prevScore)//We have went up a ranking
{
aliceRank--;
}
leaderboardIndex--;
}
else//scores[i] == alice[level]
{
leaderboardIndex--;
aliceRank = ranks[i];
}
prevScore = scores[i];
}
}
}
}