-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy path146. LRU Cache.cpp
40 lines (36 loc) · 931 Bytes
/
146. LRU Cache.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
class LRUCache {
public:
LRUCache(int capacity) {
this->capacity = capacity;
}
int get(int key) {
if(!m.count(key) || m[key] == -1) return -1;
q.push_back(key);
visited[key]++;
return m[key];
}
void put(int key, int value) {
if(!m.count(key)|| m[key] == -1) count++;
else visited[key]++;
if(count > capacity){
while(visited[q.front()]) visited[q.front()]--, q.pop_front();
m[q.front()] = -1;
q.pop_front();
count--;
}
q.push_back(key);
m[key] = value;
}
private:
unordered_map<int, int>m;
unordered_map<int, int>visited;
deque<int>q;
int count = 0;
int capacity = 0;
};
/**
* 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);
*/