《每日一题》——146. LRU 缓存|460. LFU 缓存

class LRUCache {
public:
//函数 get 和 put 必须以 O(1) 的平均时间复杂度运行
//保持把新鲜数据往链表头移动。新鲜的定义:刚被修改(put),或者访问过(get),就算新鲜,就需要放到链表头。
//过期键直接 pop_back(),链表节点越往后,越陈旧
    struct Node{
        int key, value;
        Node* left,* right;
        Node(){};
        Node(int k, int v):key(k), value(v), left(nullptr), right(nullptr){};
    };
    int n;
    unordered_map<int, Node*> mp; 
    Node* L, *R;//利用虚拟的头节点和尾节点来帮助删除和插入
    LRUCache(int capacity) {
        n = capacity;
        L = new Node(-1, -1);
        R = new Node(-1, -1);
        L->right = R;
        R->left = L;
    }
    //p->left  p  p->right
    void remove(Node* p){//删除p节点
        p->right->left = p->left;
        p->left->right = p->right;
    }
    
    //L p L->right
    void insert(Node* p){//将p节点移动到虚拟头节点后面
        p->right = L->right;
        p->left = L;
        L->right->left = p;
        L->right = p;
    }

    int get(int key) {
        if (!mp.count(key)){
            return -1;
        }
        int v = mp[key]->value;
        remove(mp[key]);
        insert(mp[key]);

        return v;
    }
    
    void put(int key, int value) {
        if (mp.count(key)){
            mp[key]->value = value;
            remove(mp[key]);
            insert(mp[key]);
        }
        else{
            if (mp.size() == n){
                Node* p = R->left;
                remove(p);
                mp.erase(p->key);
                delete p;
            }
            //否则,插入(key, value)
            Node* p = new Node(key,value);
            mp[key] = p;
            insert(p);
        }
    }
};

/**
 * Your LRUCache object will be instantiated and called as such:
 * LRUCache* obj = new LRUCache(capacity);
 * int param_1 = obj->get(key);
 * obj->put(key,value);
 */

 

struct Node{
    int key;
    int val;
    int freq;//频率
    Node* left;
    Node* right;

    Node(): key(-1), val(-1), freq(0), left(nullptr), right(nullptr){}
    Node(int _k, int _v): key(_k), val(_v), freq(1), left(nullptr), right(nullptr){}
};

struct FreqList{
    int freq;
    Node* vhead;
    Node* vtail;
    FreqList (int _f): freq(_f), vhead(new Node()), vtail(new Node()){
        vhead->right = vtail;
        vtail->left = vhead;
    }
};//虚构的头、尾节点
class LFUCache {
private:
    unordered_map<int, Node*> mp;//
    unordered_map<int, FreqList*> freq_map;//频率节点
    int n;
    int min_freq;
public:


    LFUCache(int capacity): n(capacity) {}
    
    //p->left p p->right
    void remove(Node* p){
        //操作和LRU类似
        p->right->left = p->left;
        p->left->right = p->right;
    }

    void inserthead(Node* p){//放到同频率的对头
        int freq = p->freq;
        if (freq_map.find(freq) == freq_map.end()){//没找到这个频率的
            freq_map[freq] = new FreqList(freq);
        }
        //L p L->right
        //操作和LRU类似
        FreqList* L = freq_map[freq];
        p->right = L->vhead->right;
        p->left = L->vhead;
        L->vhead->right->left = p;
        L->vhead->right = p;
    }
    bool empty(FreqList* L){
        return L->vhead->right == L->vtail ? true: false;
    }
    int get(int key) {
        int res = -1;
        if (mp.find(key) != mp.end()){//找到了
            Node* p = mp[key];
            res = p->val;
            remove(p);
            p->freq++;
            if (empty(freq_map[min_freq])) min_freq++;//生成一个频率的双向链表
            inserthead(p);
        }
        return res;
    }
    
    void put(int key, int value) {
        if (n == 0) return;
        if (get(key) != -1){
            mp[key]->val = value;
        }else{
            if (mp.size() == n){//满了
                Node* p = freq_map[min_freq]->vtail->left;
                remove(p);
                mp.erase(p->key);
                delete p;
            }
            Node* p = new Node(key, value);
            mp[key] = p;
            min_freq = 1;//新加入的频率最低为1
            inserthead(p);
        }
    }
};

/**
 * Your LFUCache object will be instantiated and called as such:
 * LFUCache* obj = new LFUCache(capacity);
 * int param_1 = obj->get(key);
 * obj->put(key,value);
 */

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值