You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Implement LRU Cache, LRU Cache means Least Recently Used Cache.
LRU Cache has a capacity that indicates max count of store.
If input into the cache when cache is full, The cache delete Least Recently Used key and set the new key-value into cache.
Use cases are get, put.
Get and Put should have a time complexity of O(1).
Approach
Using Map, we can store key-value pairs.
Also we can access to least used key by using Linked-List.
If "get" some value using key, the key's position in Linked-List is re-arranged.
When "put" some key, we can delete least recently used key using Linked-List, because the left most node is the oldest node.
There is a trick to remove node from Linked-List and insert node into Linked-List.
The trick is saving node into cache instead of value.
Problem
Implement LRU Cache, LRU Cache means Least Recently Used Cache.
LRU Cache has a capacity that indicates max count of store.
If input into the cache when cache is full, The cache delete Least Recently Used key and set the new key-value into cache.
Use cases are get, put.
Get and Put should have a time complexity of O(1).
Approach
Using Map, we can store key-value pairs.
Also we can access to least used key by using Linked-List.
If "get" some value using key, the key's position in Linked-List is re-arranged.
When "put" some key, we can delete least recently used key using Linked-List, because the left most node is the oldest node.
There is a trick to remove node from Linked-List and insert node into Linked-List.
The trick is saving node into cache instead of value.
The code is below.
The text was updated successfully, but these errors were encountered: