#include <iostream>
#include <vector>
#include <list>
#include <unordered_map>
#include <unordered_set>
using namespace std;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// LFU - Least Frequently Used
class FreqNode{
public:
int freq;
// We use a SET in lieu of a linked list for storing elements with the same access frequency
// for simplicitly of implementation.
// HASH SET structure which holds the keys of such elements that have the same access frequency.
// Its insertion, lookup and deletion runtime complexity is O(1).
unordered_set<int> items;
FreqNode(int f){
this->freq = f;
}
};
// LFU - Least Frequently Used Cache Algorithm
// 1 Linked List + 1 Set + 1 Hash
// O(1) for all: Insert, Delete and Lookup
class LFUCache{
public:
list<FreqNode*> freqNodes;
LFU(Least Frequently Used) C++ 实现
最新推荐文章于 2025-11-02 18:12:36 发布
本文介绍了一种LFU(Least Frequently Used)缓存算法的实现细节,采用链表和哈希集合的数据结构来保证插入、删除和查找操作的时间复杂度为O(1)。通过测试程序展示了该算法在特定数据集上的应用。

最低0.47元/天 解锁文章
506

被折叠的 条评论
为什么被折叠?



