【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;
}
相关推荐
welkin7 分钟前
KMP 个人理解
前端·算法
半桔13 分钟前
红黑树剖析
c语言·开发语言·数据结构·c++·后端·算法
eason_fan22 分钟前
前端面试手撕代码(字节)
前端·算法·面试
今天_也很困29 分钟前
牛客2025年愚人节比赛
c++·算法
Joe_Wang531 分钟前
[图论]拓扑排序
数据结构·c++·算法·leetcode·图论·拓扑排序
2401_858286111 小时前
CD21.【C++ Dev】类和对象(12) 流插入运算符的重载
开发语言·c++·算法·类和对象·运算符重载
Foyo Designer1 小时前
【 <二> 丹方改良:Spring 时代的 JavaWeb】之 Spring Boot 中的国际化:支持多语言的 RESTful API
java·spring boot·redis·后端·spring·缓存·restful
梭七y1 小时前
【力扣hot100题】(033)合并K个升序链表
算法·leetcode·链表
月亮被咬碎成星星1 小时前
LeetCode[383]赎金信
算法·leetcode
无难事者若执2 小时前
新手村:逻辑回归-理解03:逻辑回归中的最大似然函数
算法·机器学习·逻辑回归