-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path146.cpp
More file actions
executable file
·38 lines (35 loc) · 897 Bytes
/
146.cpp
File metadata and controls
executable file
·38 lines (35 loc) · 897 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
30
31
32
33
34
35
36
37
38
class LRUCache {
public:
deque<int> LRUq;
unordered_map<int,int> LRUm;
int own_capacity;
LRUCache(int capacity) {
own_capacity = capacity;
}
int get(int key) {
if (LRUm.count(key)){
deque<int>::iterator it = find(LRUq.begin(), LRUq.end(), key);
it = LRUq.erase(it);
LRUq.push_back(key);
return LRUm[key];
}else{
return -1;
}
}
void put(int key, int value) {
if (LRUq.size() == own_capacity){
int out = LRUq[0];
LRUm[out] = -1;
LRUq.pop_back();
}else{
LRUq.push_back(key);
LRUm.insert(key,value);
}
}
};
/**
* 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);
*/