-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsolution.java
36 lines (31 loc) · 980 Bytes
/
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
class AllOne {
private Map<String, Integer> count;
private TreeSet<Pair<Integer, String>> set;
public AllOne() {
count = new HashMap<>();
set = new TreeSet<>((a, b) -> a.getKey().equals(b.getKey()) ? a.getValue().compareTo(b.getValue())
: a.getKey() - b.getKey());
}
public void inc(String key) {
int n = count.getOrDefault(key, 0);
count.put(key, n + 1);
set.remove(new Pair<>(n, key));
set.add(new Pair<>(n + 1, key));
}
public void dec(String key) {
int n = count.get(key);
set.remove(new Pair<>(n, key));
if (n == 1)
count.remove(key);
else {
count.put(key, n - 1);
set.add(new Pair<>(n - 1, key));
}
}
public String getMaxKey() {
return set.isEmpty() ? "" : set.last().getValue();
}
public String getMinKey() {
return set.isEmpty() ? "" : set.first().getValue();
}
}