-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLRUCache.cpp
More file actions
39 lines (36 loc) · 1.02 KB
/
LRUCache.cpp
File metadata and controls
39 lines (36 loc) · 1.02 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
class LRUCache {
public:
LRUCache(int capacity): cap(capacity) {
cache.clear();
pos.clear();
}
int get(int key) {
auto it = pos.find(key);
if (it == pos.end()) return -1;
cache.splice(cache.begin(), cache, it -> second);
return it -> second -> second;
}
void put(int key, int value) {
auto it = pos.find(key);
if (pos.find(key) != pos.end()) {
it -> second -> second = value;
return cache.splice(cache.begin(), cache, it -> second);
}
cache.push_front({key, value});
pos[key] = cache.begin();
if (cache.size() > cap) {
pos.erase(cache.back().first);
cache.pop_back();
}
}
private:
int cap;
list<pair<int, int> > cache;
unordered_map<int, list<pair<int, int> >::iterator> pos;
};
/**
* 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);
*/