-
Notifications
You must be signed in to change notification settings - Fork 3
/
146.cpp
82 lines (75 loc) · 2.11 KB
/
146.cpp
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
78
79
80
81
82
class LRUCache {
public:
int capacity_;
unordered_map<int, list<pair<int, int>>::iterator> mp;
list<pair<int, int>> cache;
LRUCache(int capacity) {
capacity_ = capacity;
}
int get(int key) {
if (mp.find(key) == mp.end()) return -1;
auto it = mp[key];
cache.splice(cache.begin(), cache, it);
return it->second;
}
void put(int key, int value) {
if (mp.find(key) != mp.end()) {
auto it = mp[key];
it->second = value;
cache.splice(cache.begin(), cache, it);
return;
}
if (mp.size() == capacity_) {
auto [k, v] = cache.back();
cache.pop_back();
mp.erase(k);
}
cache.push_front(make_pair(key, value));
mp[key] = cache.begin();
}
};
/**
* 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);
*/
// V2
// class LRUCache {
// public:
// int _capacity;
// unordered_map<int, pair<int, int>> mp; // key => {value, count}
// queue<int> q; // key
// LRUCache(int capacity) {
// _capacity = capacity;
// }
// int get(int key) {
// if (mp.find(key) == mp.end()) return -1;
// mp[key].second++;
// q.push(key);
// return mp[key].first;
// }
// void put(int key, int value) {
// if (mp.find(key) != mp.end()) {
// mp[key].first = value;
// mp[key].second++;
// q.push(key);
// }
// else {
// while (mp.size() >= _capacity) {
// int k = q.front();
// q.pop();
// mp[k].second--;
// if (mp[k].second == 0) mp.erase(k);
// }
// mp[key] = make_pair(value, 1);
// q.push(key);
// }
// }
// };
// /**
// * 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);
// */