【OJ】LRU缓存、LFU缓存

leetcode 146

https://leetcode.cn/problems/lru-cache/description/

cpp 复制代码
#include <map>
#include <list>

template <typename Tkey, typename Tval>
class LRUCache {
public:
    LRUCache(int capacity) : capacity_(capacity)
    {
    }

    int get(Tkey key)
    {
        auto i = m_.find(key);
        if (i != m_.end()) {
            l_.splice(l_.begin(), l_, i->second);
            return i->second->second;
        } else {
            return Tval{-1};
        }
    }

    void put(Tkey key, Tval value)
    {
        auto i = m_.find(key);
        if (i != m_.end()) {
            l_.splice(l_.begin(), l_, i->second);
            i->second->second = value;
        } else {
            l_.insert(l_.begin(), {key, value});
            m_.insert({key, l_.begin()});
            Prune();
        }
    }

private:
    int capacity_;
    using lp = std::list<std::pair<Tkey, Tval> >;
    using lit = typename lp::iterator;
    lp l_;
    std::map<Tkey, lit> m_;
    void Prune()
    {
        if (m_.size() > capacity_) {
            auto i = l_.back();
            m_.erase(i.first);
            l_.pop_back();
        }
    }
};


int main()
{
    LRUCache<int, int> a(2);
    a.put(1, 1);
    a.put(2, 2);
    a.get(1);
    a.put(3, 3);
    a.get(2);
    return 0;
}
cpp 复制代码
#include <map>
#include <list>
#include <iostream>
#include <set>
#include <algorithm>

class LFUCache {
public:
    LFUCache(int capacity) : capacity_(capacity)
    {
    }

    int get(int key)
    {
        auto i = m_k_lit_.find(key);
        if (i == m_k_lit_.end()) {
            return -1;
        } else {
            int res = i->second->second;
            l_.splice(l_.begin(), l_, i->second);
            UpdateKeyAndCnt(key);
            return res;
        }
    }

    void put(int key, int value)
    {
        auto ik = m_k_cnt_.find(key);
        // 如果是新key,才管理容量
        if (ik == m_k_cnt_.end()) {
            if (m_k_lit_.size() == capacity_) {
                // 先删除最不常用的元素
                auto &keys = m_cnt_keys_.begin()->second;
                int cnt = m_cnt_keys_.begin()->first;
                int key = -1;
                // 有多个次数相等的元素,删除最久未用的
                if (keys.size() > 1) {
                    for (auto i = l_.rbegin(); i != l_.rend(); i++) {
                        key = i->first;
                        if (keys.find(key) != keys.end()) {
                            break;
                        }
                    }
                } else if (keys.size() > 0) {
                    key = *(keys.begin());
                }
                auto it = m_k_lit_[key];
                l_.splice(l_.begin(), l_, it);
                l_.pop_front();
                keys.erase(key);
                if (keys.size() < 1) {
                    m_cnt_keys_.erase(cnt);
                }
                m_k_cnt_.erase(key);
                m_k_lit_.erase(key);
            }
            l_.push_front({key, value});
            m_k_lit_.insert({key, l_.begin()});
            m_k_cnt_.insert({key, 1});
            AddCntKey(1, key);
        } else {
            l_.splice(l_.begin(), l_, m_k_lit_[key]);
            l_.front().second = value;
            UpdateKeyAndCnt(key);
        }
    }

private:
    int capacity_;
    using key = int;
    using val = int;
    using cnt = int;
    using lp = typename std::list<std::pair<key, val> >;
    lp l_;
    std::map<key, lp::iterator> m_k_lit_;
    std::map<key, cnt> m_k_cnt_;
    std::map<cnt, std::set<key> > m_cnt_keys_;
    void UpdateKeyAndCnt(int key)
    {
        m_k_lit_[key] = l_.begin();
        int cnt_pre = m_k_cnt_[key];
        m_k_cnt_[key]++;
        DeleteCnt(cnt_pre, key);
        int cnt_new = m_k_cnt_[key];
        AddCntKey(cnt_new, key);
    }
    void DeleteCnt(int cnt_pre, int key)
    {
        m_cnt_keys_[cnt_pre].erase(key);
        if (m_cnt_keys_[cnt_pre].size() < 1) {
            m_cnt_keys_.erase(cnt_pre);
        }
    }
    void AddCntKey(int cnt, int key)
    {
        auto ic = m_cnt_keys_.find(cnt);
        if (ic == m_cnt_keys_.end()) {
            m_cnt_keys_.insert({cnt, {key}});
        } else {
            ic->second.insert(key);
        }
    }
};

int main()
{
    LFUCache lfu(2);
    lfu.get(2);
    lfu.put(2, 6);
    lfu.get(1);
    lfu.put(1, 5);
    lfu.put(1, 2);
    lfu.get(1);
    lfu.get(2);
    return 0;
}
相关推荐
雾月5543 分钟前
LeetCode 1292 元素和小于等于阈值的正方形的最大边长
java·数据结构·算法·leetcode·职场和发展
Pasregret2 小时前
多级缓存架构深度解析:从设计原理到生产实践
缓存·架构
知来者逆2 小时前
计算机视觉——速度与精度的完美结合的实时目标检测算法RF-DETR详解
图像处理·人工智能·深度学习·算法·目标检测·计算机视觉·rf-detr
阿让啊2 小时前
C语言中操作字节的某一位
c语言·开发语言·数据结构·单片机·算法
এ᭄画画的北北2 小时前
力扣-160.相交链表
算法·leetcode·链表
爱研究的小陈3 小时前
Day 3:数学基础回顾——线性代数与概率论在AI中的核心作用
算法
渭雨轻尘_学习计算机ing3 小时前
二叉树的最大宽度计算
算法·面试
BB_CC_DD4 小时前
四. 以Annoy算法建树的方式聚类清洗图像数据集,一次建树,无限次聚类搜索,提升聚类搜索效率。(附完整代码)
深度学习·算法·聚类
我的golang之路果然有问题4 小时前
快速了解redis,个人笔记
数据库·经验分享·redis·笔记·学习·缓存·内存
道友老李5 小时前
【存储中间件】Redis核心技术与实战(五):Redis缓存使用问题(BigKey、数据倾斜、Redis脑裂、多级缓存)、互联网大厂中的Redis
redis·缓存·中间件