LFU

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

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 inO(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

Note

比较复杂,需要两个哈希表存储键映射和频率映射

countMap存储的value是哈希set链表,来决定当tie的情况出现的most recent的顺序

min存储的全局最least的频率

操作就是基于这些表和变量的维护:

  • Get操作,都要移除/更新原来的countMap的LinkedHashSet:

    • 这个key是最小的频率,且value空了:那么节约空间,把整个key-value删去

    • 然后更新countMap的频率,加入LinkedHashSet

  • Put操作:

    • 如果存在,更新一下,然后退出

    • 如果满了,就evicting the least freq one,那么遍历min对应的LinkedHashSet,然后删最least recent的,如果删空了,节约空间,就把删整个key-value

    • 最后就是创建新的的情况,min就是它,它就是1,加入1对应的LinkedHashSet

总结就是不停更新countMap,表值的LinkedHashSet来handle最不recent的情况

Code

Last updated