-
Notifications
You must be signed in to change notification settings - Fork 2
/
Solution.java
54 lines (48 loc) · 1.22 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
/**
TreeSet
lastIndex
previousIndex
increment index or lower index
*/
class SORTracker {
TreeSet<Location> set = new TreeSet<>();
Location lastReturned = new Location("", Integer.MAX_VALUE);
public SORTracker() {
}
/**
Time complexity: O(nlogn)
Space complexity: O(n)
*/
public void add(String name, int score) {
Location l = new Location(name, score);
set.add(l);
if (l.compareTo(lastReturned) < 0) {
lastReturned = set.lower(lastReturned);
}
}
/**
Time complexity: O(n)
Space complexity: O(1)
*/
public String get() {
lastReturned = set.higher(lastReturned);
return lastReturned.name;
}
private class Location implements Comparable<Location> {
String name;
int score;
Location(String n, int s) {
name = n;
score = s;
}
public int compareTo(Location l) {
return score != l.score ? -Integer.compare(score, l.score) : name.compareTo(l.name);
}
}
}
/**
* Your SORTracker object will be instantiated and called as such:
* SORTracker obj = new SORTracker();
* obj.add(name,score);
* String param_2 = obj.get();
*/