【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;
}
相关推荐
自身就是太阳5 分钟前
2024蓝桥杯省B好题分析
算法·职场和发展·蓝桥杯
孙小二写代码28 分钟前
[leetcode刷题]面试经典150题之1合并两个有序数组(简单)
算法·leetcode·面试
little redcap34 分钟前
第十九次CCF计算机软件能力认证-1246(过64%的代码-个人题解)
算法
David猪大卫1 小时前
数据结构修炼——顺序表和链表的区别与联系
c语言·数据结构·学习·算法·leetcode·链表·蓝桥杯
Iceberg_wWzZ1 小时前
数据结构(Day14)
linux·c语言·数据结构·算法
夏天天天天天天天#1 小时前
求Huffman树及其matlab程序详解
算法·matlab·图论
Infedium1 小时前
优数:助力更高效的边缘计算
算法·业界资讯
student.J1 小时前
傅里叶变换
python·算法·傅里叶
五味香2 小时前
C++学习,动态内存
java·c语言·开发语言·jvm·c++·学习·算法
Beauty.5682 小时前
P1328 [NOIP2014 提高组] 生活大爆炸版石头剪刀布
数据结构·c++·算法