460. LFU Cache
- Total accepted:5305
- Total submissions:26292
- Difficulty:hard
- Contributors: 1337c0d3r , fishercoder
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
Main topic:
Implements the data structure for the "least frequently used cache" (LFU cache) design. The get and set operations should be supported.
Get (key)-returns its corresponding value if a key exists, otherwise 1.
Set (key, value)-adds value if no key exists, otherwise replaces the original value. When the cache is full, the least frequently used items should be removed. If there are multiple items with the same frequency, remove the most recently used (Least recently used) project.
Further thinking:
Can I complete the operation at O (1) time?
Problem Solving Ideas:
Doubly linked list (doubly Linked list) + hash table (hash table)
First, you define a doubly linked list node: Keynode (Key node) and Freqnode (Frequency node).
Keynode Save Key (key), value (value), freq (frequency), prev (precursor), Next (successor)Freqnode save Freq (Frequency), prev (precursor), next (successor), First (pointing to the newest keynode), last (pointing to the oldest keynode)
The following properties are maintained in the data structure Lfucache:
Capacity: Cache capacity keydict: Mapping from key to Keynode freqdict: mapping from Freq to Freqnode Head: point to the smallest freqnode
The overall data structure is designed as shown in:
Head---FreqNode1----FreqNode2----...----freqnoden | | | First First First first | | | Keynodea keynodee keynodeg | | | Keynodeb keynodef keynodeh | | | Keynodec last keynodei | | keynoded last | Last
The Lfucache operation is implemented as follows:
Set (key, value):
If capacity is 0, ignoring the current operation, end If Keydict contains key, replace its value, update node frequency, end otherwise if the current keydict length = = capcity, Remove Head.last (lowest frequency and oldest keynode) add Keynode (key, value), add keydict, and update freqdict
Get (Key):
If the keydict contains key, then update the node frequency, return the corresponding value otherwise, return-1
Update of Node Frequency:
Find the corresponding keynode from the keydict, and then through the freq value of Keynode, find the corresponding freqdict from Freqnode if the Freqnode next node is not equal to Freq + 1, insert a value to the right of Freq + 1 of the new Freqnode node will keynode the Freq value +1, from the current Keynode linked list to the new freqnode corresponding keynode linked list if Keynode moved, the original Freqnode the corresponding Keynode linked list is empty, Delete the original Freqnode after the operation is complete, if the head changes are involved, update the head
460. LFU Cache