forked from striver79/SDESheet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LFUCacheCppLeetcode
118 lines (109 loc) · 3.05 KB
/
LFUCacheCppLeetcode
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
struct Node {
int key, value, cnt;
Node *next;
Node *prev;
Node(int _key, int _value) {
key = _key;
value = _value;
cnt = 1;
}
};
struct List {
int size;
Node *head;
Node *tail;
List() {
head = new Node(0, 0);
tail = new Node(0,0);
head->next = tail;
tail->prev = head;
size = 0;
}
void addFront(Node *node) {
Node* temp = head->next;
node->next = temp;
node->prev = head;
head->next = node;
temp->prev = node;
size++;
}
void removeNode(Node* delnode) {
Node* delprev = delnode->prev;
Node* delnext = delnode->next;
delprev->next = delnext;
delnext->prev = delprev;
size--;
}
};
class LFUCache {
map<int, Node*> keyNode;
map<int, List*> freqListMap;
int maxSizeCache;
int minFreq;
int curSize;
public:
LFUCache(int capacity) {
maxSizeCache = capacity;
minFreq = 0;
curSize = 0;
}
void updateFreqListMap(Node *node) {
keyNode.erase(node->key);
freqListMap[node->cnt]->removeNode(node);
if(node->cnt == minFreq && freqListMap[node->cnt]->size == 0) {
minFreq++;
}
List* nextHigherFreqList = new List();
if(freqListMap.find(node->cnt + 1) != freqListMap.end()) {
nextHigherFreqList = freqListMap[node->cnt + 1];
}
node->cnt += 1;
nextHigherFreqList->addFront(node);
freqListMap[node->cnt] = nextHigherFreqList;
keyNode[node->key] = node;
}
int get(int key) {
if(keyNode.find(key) != keyNode.end()) {
Node* node = keyNode[key];
int val = node->value;
updateFreqListMap(node);
return val;
}
return -1;
}
void put(int key, int value) {
if (maxSizeCache == 0) {
return;
}
if(keyNode.find(key) != keyNode.end()) {
Node* node = keyNode[key];
node->value = value;
updateFreqListMap(node);
}
else {
if(curSize == maxSizeCache) {
List* list = freqListMap[minFreq];
keyNode.erase(list->tail->prev->key);
freqListMap[minFreq]->removeNode(list->tail->prev);
curSize--;
}
curSize++;
// new value has to be added who is not there previously
minFreq = 1;
List* listFreq = new List();
if(freqListMap.find(minFreq) != freqListMap.end()) {
listFreq = freqListMap[minFreq];
}
Node* node = new Node(key, value);
listFreq->addFront(node);
keyNode[key] = node;
freqListMap[minFreq] = listFreq;
}
}
};
/**
* Your LFUCache object will be instantiated and called as such:
* LFUCache* obj = new LFUCache(capacity);
* int param_1 = obj->get(key);
* obj->put(key,value);
*/