-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path146.cpp
61 lines (53 loc) · 1.59 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
// 146. LRU Cache - https://leetcode.com/problems/lru-cache
#include "bits/stdc++.h"
using namespace std;
class LRUCache {
public:
LRUCache(int capacity) {
capacity_ = capacity;
}
// Get the value (will always be positive) of the key
// if the key exists in the cache, otherwise return -1.
int get(int key) {
if (!cache_.count(key)) {
return -1;
}
touch(key);
return cache_[key].first;
}
// Set or insert the value if the key is not already present.
// When the cache reached its capacity, it should invalidate the
// least recently used item before inserting a new item.
void put(int key, int value) {
// Update the value and then touch.
if (cache_.count(key)) {
cache_[key].first = value;
touch(key);
return;
}
if ((int)queue_.size() == capacity_) {
int key_to_delete = queue_.back();
queue_.pop_back();
cache_.erase(key_to_delete);
}
queue_.push_front(key);
cache_[key] = {value, queue_.begin()};
}
// Insert node to the front of the linked list
void touch(int key) {
auto it_to_delete = cache_[key].second;
queue_.erase(it_to_delete);
queue_.push_front(key);
cache_[key].second = queue_.begin();
}
private:
// key -> {value, iterator of LL}
unordered_map<int, pair<int, list<int>::iterator> > cache_;
// LL of keys
list<int> queue_;
int capacity_;
};
int main() {
ios::sync_with_stdio(false);
return 0;
}