-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLRUCache.cpp
53 lines (49 loc) · 1.56 KB
/
LRUCache.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
/* Coding challenge #146 from LeetCode */
/* get() and put() are both O(1) time complexity */
class LRUCache {
public:
LRUCache(int capacity) {
this->capacity = capacity;
}
int get(int key) {
auto it = hashmap.find(key);
if (it == hashmap.end()) {
return -1;
}
auto itList = it->second;
int value = itList->second;
cache.erase(it->second);
pair<int,int> p = make_pair(key,value);
cache.push_front(p);
itList = cache.begin();
it->second = itList;
return value;
}
void put(int key, int value) {
auto it = hashmap.find(key);
if (it == hashmap.end()) {
pair<int,int> p = make_pair(key,value);
cache.push_front(p);
auto itList = cache.begin();
hashmap.insert(make_pair(key,itList));
if (cache.size() > capacity) {
pair<int,int> old_pair = cache.back();
auto itMap = hashmap.find(old_pair.first);
hashmap.erase(itMap);
cache.pop_back();
}
}
else {
auto itList = it->second;
cache.erase(itList);
pair<int,int> p = make_pair(key,value);
cache.push_front(p);
itList = cache.begin();
it->second = itList;
}
}
private:
list<pair<int,int>> cache; // key-value pair
unordered_map<int,list<pair<int,int>>::iterator> hashmap;
int capacity;
};