LFU(Least Frequently Used) C++ 实现

本文介绍了一种LFU(Least Frequently Used)缓存算法的实现细节,采用链表和哈希集合的数据结构来保证插入、删除和查找操作的时间复杂度为O(1)。通过测试程序展示了该算法在特定数据集上的应用。
#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;
       
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值