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 ('ll 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 is 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., b or more keys, which has the same frequency), the least rece Ntly used key would be evicted.
Follow up:
Could 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 1cache.put (3, 3); Evicts key 2cache.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 3cache.get (4); Returns 4
S
[Leetcode] LFU cache Most infrequently used page substitution buffers