5252/* *
5353 * 实现方法:双哈希表
5454 *
55+ * 定义两个哈希表,第一个 freq_table 以频率 freq 为索引,每个索引存放一个双向链表,这个链表里存放所有使用频率为 freq 的缓存,
56+ * 缓存里存放三个信息,分别为键 key, 值 val 和 使用频率 freq。第二个 key_table 以键值 key 为索引,每个索引存放对应缓存在
57+ * freq_table 中链表里的内存地址,这样我们就能利用两个哈希表来使得两个操作的时间复杂度均为 O(1)。同时需要记录一个当前缓存最少
58+ * 使用的频率minFreq, 这是为了删除操作服务的。
59+ *
60+ * 对于 get(key) 操作,我们能通过索引 key 在 key_table 中找到缓存在 freq_table 中的链表的内存地址,如果不存在直接返回 -1,
61+ * 否则我们能获取到对应缓存的相关信息,这样我们就能知道缓存的键值还有使用频率,直接返回 key 对应的值即可。
62+ * 但是我们注意到 get 操作后这个缓存的使用频率加一了,所以我们需要更新缓存在哈希表 freq_table 中的位置。已知这个缓存的键 key,
63+ * 值 value,以及使用频率 freq,那么该缓存应该存放到 freq_table 中 freq + 1 索引下的链表中。所以我们在当前链表中 O(1)
64+ * 删除该缓存对应的节点,根据情况更新 minFreq 值,然后将其 O(1) 插入到 freq + 1 索引下的链表头完成更新。插入到链表头,
65+ * 其实是为了保证缓存在当前链表中从链表头到链表尾的插入时间是有序的,为下面的删除操作服务。
66+ *
67+ * 对于 put(key, value) 操作,我们先通过索引 key在 key_table 中查看是否有对应的缓存,如果有的话,其实操作等价于 get(key)
68+ * 操作,唯一的区别就是我们需要将当前的缓存里的值更新为 value。如果没有的话,相当于是新加入的缓存,如果缓存已经到达容量,
69+ * 需要先删除最近最少使用的缓存,再进行插入。
70+ *
71+ * 先考虑插入,由于是新插入的,所以缓存的使用频率一定是 1,所以我们将缓存的信息插入到 freq_table 中 1 索引下的列表头即可,
72+ * 同时更新 key_table[key] 的信息,以及更新 minFreq = 1。
73+ *
74+ * 对于删除操作,由于我们实时维护了 minFreq,所以我们能够知道 freq_table 里目前最少使用频率的索引,同时因为我们保证了链表
75+ * 中从链表头到链表尾的插入时间是有序的,所以 freq_table[minFreq] 的链表中链表尾的节点即为使用频率最小且插入时间最早的节点,
76+ * 我们删除它同时根据情况更新 minFreq ,整个时间复杂度均为 O(1)。
77+ *
78+ * 复杂度:
79+ * 时间复杂度:get 时间复杂度 O(1),put 时间复杂度 O(1)。由于两个操作从头至尾都只利用了哈希表的插入删除还有链表的插入删除,
80+ * 且它们的时间复杂度均为 O(1),所以保证了两个操作的时间复杂度均为O(1)。
81+ * 空间复杂度:
82+ * O(capacity),其中 capacity 为 LFU 的缓存容量。哈希表中不会存放超过缓存容量的键值对。
83+ *
5584 */
5685
5786#include < list>
@@ -70,28 +99,110 @@ struct Node
7099class LFUCache
71100{
72101private:
73- int minfreq , capacity;
102+ int minFreq , capacity;
74103 unordered_map<int , list<Node>::iterator> key_table;
75104 unordered_map<int , list<Node>> freq_table;
76105
77106public:
78107 LFUCache (int _capacity)
79108 {
80- minfreq = 0 ;
109+ minFreq = 0 ;
81110 capacity = _capacity;
111+ key_table.clear ();
112+ freq_table.clear ();
82113 }
83114
84115 int get (int key)
85116 {
117+ if (capacity == 0 )
118+ return -1 ;
119+
120+ auto item = key_table.find (key);
121+
122+ if (item == key_table.end ())
123+ return -1 ;
124+
125+ list<Node>::iterator node = item->second ;
126+ int val = node->val , freq = node->freq ;
127+ freq_table[freq].erase (node);
128+
129+ // 如果当前链表为空,我们需要在哈希表中删除,且更新minFreq
130+ if (freq_table[freq].size () == 0 )
131+ {
132+ freq_table.erase (freq);
133+ if (minFreq == freq)
134+ minFreq += 1 ;
135+ }
136+
137+ // 插入到 freq + 1 中
138+ freq_table[freq + 1 ].push_front (Node (key, val, freq + 1 ));
139+ key_table[key] = freq_table[freq + 1 ].begin ();
140+
141+ return val;
86142 }
87143
88144 void put (int key, int value)
89145 {
146+ if (capacity == 0 )
147+ return ;
148+
149+ auto item = key_table.find (key);
150+
151+ if (item == key_table.end ())
152+ {
153+ // 如果缓存已满,需要进行删除操作
154+ if (key_table.size () == capacity)
155+ {
156+ // 通过 minFreq 获取 freq_table[minFreq] 链表的末尾节点
157+ auto item2 = freq_table[minFreq].back ();
158+ key_table.erase (item2.key );
159+ freq_table[minFreq].pop_back ();
160+
161+ if (freq_table[minFreq].size () == 0 )
162+ {
163+ freq_table.erase (minFreq);
164+ }
165+ }
166+
167+ freq_table[1 ].push_front (Node (key, value, 1 ));
168+ key_table[key] = freq_table[1 ].begin ();
169+ minFreq = 1 ;
170+ }
171+ else
172+ {
173+ list<Node>::iterator node = item->second ;
174+ int freq = node->freq ;
175+ freq_table[freq].erase (node);
176+
177+ if (freq_table[freq].size () == 0 )
178+ {
179+ freq_table.erase (freq);
180+ if (minFreq == freq)
181+ minFreq += 1 ;
182+ }
183+
184+ freq_table[freq + 1 ].push_front (Node (key, value, freq + 1 ));
185+ key_table[key] = freq_table[freq + 1 ].begin ();
186+ }
90187 }
91188};
92189
93190int main (int argc, char const *argv[])
94191{
95- /* code */
192+ // cnt(x) = 键 x 的使用计数
193+ // cache=[] 将显示最后一次使用的顺序(最左边的元素是最近的)
194+
195+ LFUCache lfu = LFUCache (2 );
196+
197+ lfu.put (1 , 1 ); // cache=[1,_], cnt(1)=1
198+ lfu.put (2 , 2 ); // cache=[2,1], cnt(2)=1, cnt(1)=1
199+
200+ std::cout << " lru.get(1) = " << lfu.get (1 ) << std::endl; // 返回 1
201+ // cache=[1,2], cnt(2)=1, cnt(1)=2
202+ lfu.put (3 , 3 ); // 去除键 2 ,因为 cnt(2)=1 ,使用计数最小
203+ // cache=[3,1], cnt(3)=1, cnt(1)=2
204+ std::cout << " lru.get(2) = " << lfu.get (2 ) << std::endl; // 返回 -1(未找到)
205+ std::cout << " lru.get(3) = " << lfu.get (3 ) << std::endl; // 返回 3
206+
96207 return 0 ;
97208}
0 commit comments