-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path146.cpp
More file actions
65 lines (61 loc) · 1.59 KB
/
Copy path146.cpp
File metadata and controls
65 lines (61 loc) · 1.59 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
class LRUCache {
struct KV {
int key, val;
KV *prev, *next;
KV(int k, int v) : key(k), val(v), prev(0), next(0) {}
};
KV head, tail;
unordered_map<int, KV *> km;
int cap;
void update(KV *kv) {
kv->next->prev = kv->prev;
kv->prev->next = kv->next;
kv->next = head.next;
kv->prev = &head;
head.next = kv;
kv->next->prev = kv;
}
public:
LRUCache(int capacity) : head(0, 0), tail(0, 0) {
head.next = &tail;
tail.prev = &head;
cap = capacity;
}
int get(int key) {
auto p = km.find(key);
if (p != km.end()) {
update(p->second);
return p->second->val;
}
return -1;
}
void put(int key, int value) {
auto p = km.find(key);
if (p != km.end()) {
update(p->second);
p->second->val = value;
} else {
if (km.size() >= cap) {
KV *v = tail.prev;
km.erase(v->key);
v->val = value;
v->key = key;
update(v);
km.insert(make_pair(key, v));
} else {
KV *v = new KV(key, value);
v->next = head.next;
v->next->prev = v;
v->prev = &head;
head.next = v;
km.insert(make_pair(key, v));
}
}
}
};
/**
* Your LRUCache object will be instantiated and called as such:
* LRUCache obj = new LRUCache(capacity);
* int param_1 = obj.get(key);
* obj.put(key,value);
*/