-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path146. LRU Cache
77 lines (76 loc) · 2.05 KB
/
146. LRU Cache
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
public class LRUCache {
public class DLNode{
int key;
int value;
DLNode pre;
DLNode post;
}
Map<Integer, DLNode> cache;
DLNode head;
DLNode tail;
int count;
int capacity;
//Constructor for LRU Cache
public LRUCache(int capacity) {
cache = new HashMap();
this.capacity = capacity;
this.count = 0;
head = new DLNode();
tail = new DLNode();
head.pre = null;
tail.post = null;
head.post = tail;
tail.pre = head;
}
//4 Helper methods
public DLNode poptail(){
DLNode node = tail.pre;
remove(node);
return node;
}
public void refresh(DLNode node){
remove(node);
insert(node);
}
public void remove(DLNode node){
DLNode pre = node.pre;
DLNode post = node.post;
pre.post = post;
post.pre = pre;
}
//Insert just after the head
public void insert(DLNode node){
node.pre = head;
node.post = head.post;
head.post.pre = node;
head.post = node;
}
//Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
public int get(int key) {
DLNode ret = cache.get(key);
if(ret==null) return -1;
refresh(ret);
return ret.value;
}
//Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.
public void put(int key, int value) {
DLNode ret = cache.get(key);
if(ret==null){
DLNode node = new DLNode();
node.key = key;
node.value = value;
cache.put(key,node);
insert(node);
count++;
if(count>capacity){
DLNode un = poptail();
cache.remove(un.key);
count--;
}
}
else{
ret.value = value;
refresh(ret);
}
}
}