-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathLRUCache.cpp
More file actions
29 lines (29 loc) · 849 Bytes
/
Copy pathLRUCache.cpp
File metadata and controls
29 lines (29 loc) · 849 Bytes
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
// We'll use built-in list for O(1) splice.
class LRUCache {
int cap;
list<pair<int,int>> dll; // key,val - front most recent
unordered_map<int, list<pair<int,int>>::iterator> mp;
public:
LRUCache(int capacity): cap(capacity) {}
int get(int key) {
auto it = mp.find(key);
if (it == mp.end()) return -1;
auto nodeIt = it->second;
int val = nodeIt->second;
dll.erase(nodeIt);
dll.push_front({key, val});
mp[key] = dll.begin();
return val;
}
void put(int key, int value) {
if (mp.count(key)) {
dll.erase(mp[key]);
} else if (dll.size() == cap) {
auto last = dll.back();
mp.erase(last.first);
dll.pop_back();
}
dll.push_front({key, value});
mp[key] = dll.begin();
}
};