Skip to content

Commit ce475ca

Browse files
committed
LFU_Cache in cpp updated
1 parent 9fa4eee commit ce475ca

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

Linked List/LFU_Cache.cpp

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Question--> LFU Cache
2+
// Design and implement a data structure for a Least Frequently Used (LFU) cache.
3+
4+
// Implement the LFUCache class:
5+
6+
// LFUCache(int capacity) Initializes the object with the capacity of the data structure.
7+
// int get(int key) Gets the value of the key if the key exists in the cache. Otherwise, returns -1.
8+
// void put(int key, int value) Update the value of the key if present, or inserts the key if not already present. When the cache reaches its capacity, it should invalidate and remove the least frequently used key before inserting a new item. For this problem, when there is a tie (i.e., two or more keys with the same frequency), the least recently used key would be invalidated.
9+
// To determine the least frequently used key, a use counter is maintained for each key in the cache. The key with the smallest use counter is the least frequently used key.
10+
11+
// When a key is first inserted into the cache, its use counter is set to 1 (due to the put operation). The use counter for a key in the cache is incremented either a get or put operation is called on it.
12+
13+
// The functions get and put must each run in O(1) average time complexity
14+
15+
16+
// solution -->
17+
18+
19+
#include <bits/stdc++.h>
20+
using namespace std;
21+
22+
23+
class LFUCache {
24+
int cap;
25+
int size;
26+
int minFreq;
27+
unordered_map<int, pair<int, int>> m; //key to {value,freq};
28+
unordered_map<int, list<int>::iterator> mIter; //key to list iterator;
29+
unordered_map<int, list<int>> fm; //freq to key list;
30+
public:
31+
LFUCache(int capacity) {
32+
// initialize your data structure here
33+
cap=capacity;
34+
size=0;
35+
}
36+
37+
int get(int key) {
38+
if(m.count(key)==0) return -1; // if key not found return -1;
39+
40+
fm[m[key].second].erase(mIter[key]); //erase from old freq list
41+
m[key].second++; // increase freq;
42+
fm[m[key].second].push_back(key); // add to new freq list;
43+
mIter[key]=--fm[m[key].second].end();// point to new freq list;
44+
45+
// if old min freq list empty, increase min freq;
46+
if(fm[minFreq].size()==0 )
47+
minFreq++;
48+
49+
return m[key].first; // return value
50+
}
51+
52+
void put(int key, int value) {
53+
if(cap<=0) return; // corner case
54+
55+
int storedValue=get(key); // get value if already present
56+
57+
// if already present update value, increase freq;
58+
if(storedValue!=-1)
59+
{
60+
m[key].first=value;
61+
return;
62+
}
63+
64+
// if capacity full, erase least freq used element from this list
65+
if(size>=cap )
66+
{
67+
m.erase( fm[minFreq].front() );
68+
mIter.erase( fm[minFreq].front() );
69+
fm[minFreq].pop_front();
70+
size--;
71+
}
72+
73+
m[key]={value, 1}; // insert new key value pair with freq 1;
74+
fm[1].push_back(key); // add to freq list 1;
75+
mIter[key]=--fm[1].end(); // point to new freq list;
76+
minFreq=1; // since new element added min freq will be 1;
77+
size++; // increase size
78+
}
79+
};

0 commit comments

Comments
 (0)