【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;
}
相关推荐
丶Darling.36 分钟前
Day119 | 灵神 | 二叉树 | 二叉树的最近共公共祖先
数据结构·c++·算法·二叉树
L_cl1 小时前
【Python 算法零基础 3.递推】
算法
int型码农2 小时前
数据结构第七章(四)-B树和B+树
数据结构·b树·算法·b+树
先做个垃圾出来………2 小时前
汉明距离(Hamming Distance)
开发语言·python·算法
小羊在奋斗3 小时前
【LeetCode 热题 100】二叉树的最大深度 / 翻转二叉树 / 二叉树的直径 / 验证二叉搜索树
算法·leetcode·职场和发展
·云扬·3 小时前
【PmHub后端篇】PmHub 中缓存与数据库一致性的实现方案及分析
数据库·缓存
2301_794461574 小时前
力扣-283-移动零
算法·leetcode·职场和发展
编程绿豆侠4 小时前
力扣HOT100之二叉树:98. 验证二叉搜索树
算法·leetcode·职场和发展
技术流浪者4 小时前
C/C++实践(十)C语言冒泡排序深度解析:发展历史、技术方法与应用场景
c语言·数据结构·c++·算法·排序算法
I AM_SUN5 小时前
98. 验证二叉搜索树
数据结构·c++·算法·leetcode