【OJ】LRU缓存、LFU缓存

leetcode 146

https://leetcode.cn/problems/lru-cache/description/
在这里插入图片描述

#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;
}

在这里插入图片描述

#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值