-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathSequentially Ordinal Rank Tracker.java
44 lines (39 loc) · 1.22 KB
/
Sequentially Ordinal Rank Tracker.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
// Runtime: 996 ms (Top 5.13%) | Memory: 131.3 MB (Top 81.29%)
class SORTracker {
private TreeMap<Integer, List<String>> map;
private int queryNum;
// Find suitable position for name in the list
private int getIndex(String name, List<String> list) {
int l = 0, r = list.size() - 1, m = 0;
while (l < r) {
m = l + (r - l) / 2;
if(name.compareTo(list.get(m)) > 0) {
l = m + 1;
} else {
r = m;
}
}
return name.compareTo(list.get(l)) > 0 ? l+1 : l;
}
public SORTracker() {
map = new TreeMap<>((a,b) -> (b-a));
queryNum = 0;
}
public void add(String name, int score) {
List<String> list = map.getOrDefault(score, new ArrayList<>());
int index = (list.size() == 0) ? 0 : getIndex(name, list);
list.add(index, name);
map.put(score, list);
}
public String get() {
int index = queryNum;
for (int score: map.keySet()) {
if (index < map.get(score).size()) {
queryNum++;
return map.get(score).get(index);
}
index -= map.get(score).size();
}
return "";
}
}