forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path460.LFU Cache.cpp
56 lines (53 loc) · 1.36 KB
/
460.LFU 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class LFUCache {
int cap, minfreq;
unordered_map<int,pair<int,int>>Map; // key -> value, freq;
unordered_map<int,list<int>>freq; // freq -> keys
unordered_map<int,list<int>::iterator>iter; // key -> iterator
public:
LFUCache(int capacity)
{
cap=capacity;
}
int get(int key)
{
if (Map.find(key)==Map.end()) return -1;
int f=Map[key].second;
freq[f].erase(iter[key]);
freq[f+1].push_back(key);
iter[key]=--freq[f+1].end();
Map[key].second++;
if (freq[minfreq].size()==0)
minfreq++;
return Map[key].first;
}
void put(int key, int value)
{
if (cap<=0) return;
if (get(key)!=-1)
{
Map[key].first=value;
return;
}
else
{
if (Map.size()>=cap)
{
int minKey=freq[minfreq].front();
iter.erase(minKey);
Map.erase(minKey);
freq[minfreq].pop_front();
}
Map[key]={value,1};
freq[1].push_back(key);
iter[key]=--freq[1].end();
minfreq=1;
return;
}
}
};
/**
* 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);
*/