LRU Cache
ID: 134; hard; LRU缓存策略
Solution 1 (Java)
Notes
We uses a singly linked list and a hash map in this solution. The linked list is basically the cache, where the head is the least recently used (LRU) and the end is the most recently used.The hash map keeps track of key and previous node pairs.
We first define a
Node
class with fields includingkey
,val
, andnext
.Then, the fields of the
LRUCache
class includes:capacity
: the full capacity of the cache as required by user input.size
: the current size of the cachedummy
: the dummy node pointing to the head of the linked listend
: the end of the linked listkeyToPrevMap
: the hash map of(key, previous node)
pairs.
The get
method
get
methodWe first check if a node with the input key
is in the map. If not, we return -1 not found. If we do find a node associated with that key, we move that node to the end of the linked list and return its value. (moveToEnd
method to be completed later)
The set
method
set
methodWe have three cases for setting key/value pairs.
The key is already contained in the map. All we need to do is find that node and change its value.
Within in the capacity limit, we can directly add a new node to the end of the linked list. Do not forget to update the
end
, themap
, and thesize
.When the capacity limit will be exceeded, we remove the first/LRU node from the
map
. Next, we reuse that first node by putting the new key and value into that node. Then, we move that node to the end of the linked list, meaning that it is the most recently used. Do not forget to update the map as well (for changing the head of the list).
The moveToEnd
method
moveToEnd
methodWe take in a key
and want to move the node associated with that key to the end of the linked list. We first remove it from where it currently is, i.e., pointing its previous node to its next node. Then, we add it at the end of the list. More importantly, do not forget to update the map
since two positions in the linked list have changed and do not forget to update end as well.
Solution 2 (Java)
Notes
Here we used a doubly linked list.
Note that
head
andtail
are not actually the head and tail of the doubly linked list.Head
is the node before the actual head andtail
is the node after the actual tail of the list.
Last updated