-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path146. LRU Cache.cpp
More file actions
51 lines (45 loc) · 1.29 KB
/
146. LRU Cache.cpp
File metadata and controls
51 lines (45 loc) · 1.29 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
#include <list>
#include <unordered_set>
#include <unordered_map>
using namespace std;
class LRUCache {
public:
LRUCache(int capacity) {
capacity_ = capacity;
}
int get(int key) {
auto it = m_.find(key);
if (it == m_.end()) return -1;
// found key
// moves the key value pair into the beginning
cache_.splice(cache_.begin(), cache_, it->second);
return it->second->second;
}
void put(int key, int value) {
auto it = m_.find(key);
if (it != m_.end()) {
it->second->second = value;
cache_.splice(cache_.begin(), cache_, it->second);
return;
}
// Not found. Ensures there is free capacity first
if (cache_.size() == capacity_) {
auto back = cache_.back();
cache_.pop_back();
m_.erase(back.first);
}
cache_.emplace_front(key, value);
m_[key] = cache_.begin();
return;
}
private:
int capacity_;
list<pair<int, int>> cache_;
unordered_map<int, list<pair<int, int>>::iterator> m_;
};
/**
* 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);
*/