-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLFU Cache.cpp
More file actions
55 lines (44 loc) · 1.45 KB
/
LFU Cache.cpp
File metadata and controls
55 lines (44 loc) · 1.45 KB
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
// https://leetcode.com/problems/lfu-cache/discuss/94516/Concise-C%2B%2B-O(1)-solution-using-3-hash-maps-with-explanation
// The main idea is to put all keys with the same frequency to a linked list so the most recent one can be evicted
class LFUCache {
int cap;
int size;
int minFreq;
unordered_map<int, pair<int, int>> m; // key to {value,freq};
unordered_map<int, list<int>::iterator> mIter; // key to list iterator;
unordered_map<int, list<int>> fm; // freq to key list;
public:
LFUCache(int capacity) {
cap = capacity;
size = 0;
minFreq = 0;
}
int get(int key) {
if (m.count(key) == 0) return -1; // if not found
fm[m[key].second].erase(mIter[key]);
m[key].second++;
fm[m[key].second].push_front(key);
mIter[key] = fm[m[key].second].begin();
if (fm[minFreq].size() == 0) minFreq++;
return m[key].first;
}
void set(int key, int value) {
if (cap <= 0) return;
int storedValue = get(key);
if (storedValue != -1) {
m[key].first = value;
return;
}
if (size >= cap) {
m.erase(fm[minFreq].back());
mIter.erase(fm[minFreq].back());
fm[minFreq].pop_back();
size--;
}
m[key] = {value, 1};
fm[1].push_front(key);
mIter[key] = fm[1].begin();
minFreq = 1;
size++;
}
};