LFU Cache

LFU: least frequently used (LFU) page-replacement algorithm

https://leetcode.com/problems/lfu-cache/?tab=Description

题目描述

Design and implement a data structure for Least Frequently Used (LFU) cache. It should support the following operations: get and put.

get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1. 
put(key, value) - Set or insert the value if the key is not already present. When the cache reaches its capacity, it should invalidate the least frequently used item before inserting a new item. For the purpose of this problem, when there is a tie (i.e., two or more keys that have the same frequency), the least recently used key would be evicted.

Follow up: 
Could you do both operations in O(1) time complexity?

Example:

LFUCache cache = new LFUCache( 2 /* capacity */ );

cache.put(1, 1);
cache.put(2, 2);
cache.get(1);       // returns 1
cache.put(3, 3);    // evicts key 2
cache.get(2);       // returns -1 (not found)
cache.get(3);       // returns 3.
cache.put(4, 4);    // evicts key 1.
cache.get(1);       // returns -1 (not found)
cache.get(3);       // returns 3
cache.get(4);       // returns 4

题目讨论,各种解决方案

数据结构设计

class LFUCache {
public:
    int size;
    int cap;
    int minfreq;
    map<int,pair<int,int>> m;//key to pair<value,freq>  
    map<int,list<int>::iterator> mIter;//key to list location , key在 list中的位置一个iterator 
    map<int,list<int>> fm;//freq to list , list存放的是所有的key, 最后的key是最近访问过的,头部的是最近没有访问的(淘汰)

public:

    LFUCache(int capacity) {
       cap = capacity;  
       size = 0;
    }

    int get(int key) {
        if(m.count(key) == 0)
            return -1;

        //key 频率加1,删除原来其在fm中的位置,插入到新的位置
        fm[m[key].second].erase(mIter[key]); 
        m[key].second ++;
        fm[m[key].second].push_back(key);

        mIter[key] = --fm[m[key].second].end(); // 当前key所在的位置

        if(fm[minfreq].size() == 0) //上面的步骤处理后,可能最小频率已经删除了数据,所以需要判断
            minfreq ++;

        return m[key].first;
    }

    void put(int key, int value) {
        if(cap <= 0)
            return ;

        /*
            调用成员方法get
            如果不存在,返回-1;
            如果已经存在,那么就会修改频数,删除旧的位置,添加到新的位置,但是值仍然是原来的,需要修改
        */
        int storeValue = get(key); 
        if(storeValue != -1)
        {
            m[key].first = value;
            return; // 直接返回
        }

        // 不存在的情况, 已经满了,需要删除频率最小,最近都没有访问过的那个key
        if(size >= cap){
            m.erase(fm[minfreq].front());
            mIter.erase(fm[minfreq].front());
            fm[minfreq].pop_front();
            size --;
        }

        pair<int, int> pr(value, 1);
        m[key] = pr;
        fm[1].push_back(key);
        mIter[key] = --fm[1].end();
        minfreq = 1;
        size ++;
    }
};

参考:

[1] 页面置换算法--LFU算法实现-O(1)时间复杂度

[2]https://leetcode.com/problems/lfu-cache/discuss/94516/Concise-C++-O(1)-solution-using-3-hash-maps-with-explanation

原文地址:https://www.cnblogs.com/dingxiaoqiang/p/8625797.html