-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain3.java
More file actions
65 lines (52 loc) · 1.42 KB
/
Main3.java
File metadata and controls
65 lines (52 loc) · 1.42 KB
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
package xiecheng;
import java.util.*;
public class Main3 {
static class Cache {
int size;
Map<Integer, Node> map;
Node[] mincount;
public Cache(int size) {
map = new HashMap<>(size);
mincount = new Node[size];
}
public void put(int key, int val) {
if (map.containsKey(key)) {
map.replace(key, new Node(key, val));
} else {
remove();
map.put(key, new Node(key, val));
Arrays.sort(mincount);
}
}
public void remove() {
if (this.map.size() <= size) {
} else {
map.remove(mincount[0].key);
}
}
public int get(int key) {
if (!map.containsKey(key)) {
return -1;
} else {
map.get(key).count++;
return map.get(key).val;
}
}
static class Node implements Comparable<Node> {
int key;
int val;
int count;
public Node(int key, int val) {
this.key = key;
this.val = val;
this.count = 0;
}
@Override
public int compareTo(Node o) {
return this.count - o.count;
}
}
}
public static void main(String[] args) {
}
}