zoukankan      html  css  js  c++  java
  • LC 460. LFU Cache

     link

    class LFUCache {
    public:
        unordered_map<int,int> ktov; // key to value
        unordered_map<int,int> ktof; //key to freq
        unordered_map<int,list<int>> ftol; // freq to list
        unordered_map<int,list<int>::iterator> ktoi; //key to iter
        int cap;
        int minfreq;
        int size;
        LFUCache(int capacity) {
            cap=capacity;
            minfreq=0;
            size=0;
        }
        
        int get(int key) {
            if(ktov.find(key)==ktov.end()) return -1;
            int prefreq=ktof[key];
            ftol[prefreq].erase(ktoi[key]);
            int newfreq=prefreq+1;
            ktof[key]=newfreq;
            ftol[newfreq].push_back(key);
            ktoi[key]=--ftol[newfreq].end();
            
            if(ftol[minfreq].empty()){
                ++minfreq;
            }
            return ktov[key];
        }
        
        void put(int key, int value) {
            if(cap<=0) return;
            if(ktov.find(key)!=ktov.end()){
                ktov[key]=value;
                int prefreq=ktof[key];
                ftol[prefreq].erase(ktoi[key]);
                int newfreq=prefreq+1;
                ktof[key]=newfreq;
                ftol[newfreq].push_back(key);
                ktoi[key]=--ftol[newfreq].end();
            
                if(ftol[minfreq].empty()){
                    ++minfreq;
                }
            }else{
                if(size>=cap){
                    --size;
                    int del=ftol[minfreq].front();
                    ftol[minfreq].pop_front();
                    ktov.erase(del);
                    ktof.erase(del);
                    ktoi.erase(del);
                }
                ktov[key]=value;
                ktof[key]=1;
                minfreq=1;
                ftol[1].push_back(key);
                ktoi[key]=--ftol[1].end();
                ++size;
            }
        }
    };
    
    /**
     * 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);
     */
  • 相关阅读:
    课堂练习02
    第五周进度条
    软件工程个人作业03
    第四周进度条
    构建之法阅读笔记02
    ---JS canvas学习笔记
    JavaScript 对象、DOM对象、jquery对象的区别、转换详解
    JQM---列车时刻查询
    JS---如何避免用户在请求时“猛击”
    HTML5 新增属性和废除属性
  • 原文地址:https://www.cnblogs.com/FEIIEF/p/12435842.html
Copyright © 2011-2022 走看看