Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Lintcode24 LFU Cache solution problem solution

Shulou Source: shulou.com Published: 2022-06-02 22:54:51 10月03日 Update

[topic description]

LFU (Least Frequently Used) is a famous cache eviction algorithm.For a cache with capacity k, if the cache is full and need to evict a key in it, the key with the lease frequently used will be kicked out.Implement set and get method for LFU cache.

LFU is a famous caching algorithm. Implement set and get in LFU

[topic link]

Http://www.lintcode.com/en/problem/lfu-cache/

[topic Analysis]

This problem allows us to implement a LRU cache. LRU is an abbreviation for Least Recently Used, which means the least used recently. Then the cache mainly has two member functions, get and set, in which the get function obtains the value by typing key. If it is successfully obtained, the pair (key, value) rises to the most commonly used position in the cache (top), and returns-1 if key does not exist. The set function inserts a pair of new (key, value). If there is the key in the original buffer, you need to delete the original one and insert the new one at the top of the buffer. If it does not exist, insert it directly to the top. If the buffer exceeds the capacity after adding a new value, you need to delete the least commonly used value, that is, the value at the bottom. We need three private variables, cap, l and m, where cap is the capacity of the buffer, l is the list of buffer contents, m is the hash table, and the mapping between the iterator that stores the key value key and the buffer items is convenient for us to find the target item in O (1) time.

[reference answer]

Http://www.jiuzhang.com/solutions/lfu-cache/

Tags: Cache function title capacity commonly used top famous successful three two between that is location key content variable size that is bottom meaning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Docker Shulou Technology MySQL Huawei