pytudes._2021.leetcode.medium._146__lru_cacheο
https://leetcode.com/problems/lru-cache/
Design a data structure that follows the constraints of a Least Recently Used (LRU) cache.
- Constraints:
1 β€ capacity β€ 3000
0 β€ key β€ 3000
0 β€ value β€ 104
At most 3 * 10**4 calls will be made to get and put.
- Examples:
>>> lRUCache = LRUCache(capacity=2) >>> lRUCache.put(key=1, value=0) # cache is {1=0} >>> lRUCache.get(key=1) 0 >>> lRUCache.put(key=1, value=1) # LRU key was 1, overwrites value at key 1, cache is {1=1} >>> lRUCache.put(key=2, value=2) # cache is {1=1, 2=2} >>> lRUCache.get(key=1) 1 >>> lRUCache.put(key=3, value=3) # LRU key was 2, evicts key 2, cache is {1=1, 3=3} >>> lRUCache.get(key=2) -1 >>> lRUCache.put(key=4, value=4) # LRU key was 1, evicts key 1, cache is {4=4, 3=3} >>> lRUCache.get(key=1) -1 >>> lRUCache.get(key=3) 3 >>> lRUCache.get(key=4) 4
Module Contentsο
Classesο
Doubly-linked list node |
|
Emulates an LRU cache, |
Functionsο
|
Removes a given node from its doubly-linked list |
- class pytudes._2021.leetcode.medium._146__lru_cache.DoublyLinkedListNode(key, val)[source]ο
Doubly-linked list node
- Attributes:
key: unique key associated with a DoublyLinkedListNode instance val: value associated the key prev: [optional] predecessor node next: [optional] successor node
- class pytudes._2021.leetcode.medium._146__lru_cache.LRUCache(capacity)[source]ο
Emulates an LRU cache,
Internally, LRUCache uses a hashmap and doubly-linked list for efficient cache insertions, deletions, and updates.
- Complexity:
n = capacity of cache Time:
get: O(1) put: O(1)
Space: O(n) (2n: n for the hash table, n (+2: dummy head and tail) for the linked list)
- Attributes:
- capacity:
an integer indicating how many entries the cache can store before an entry must be evicted
- cache:
a dictionary mapping keys to cache Nodes
- head:
a dummy DoublyLinkedListNode used as a handle to the first (MRU) cache entry
- tail:
a dummy DoublyLinkedListNode used as a handle to the last (LRU) cache entry
Inits LRUCache with designated capacity
- Parameters:
capacity (int) β
- _make_mru(cache_entry)[source]ο
Moves given cache entry to the MRU position in the LRU list
Implicitly right-shifting all other cache entries in the LRU list
- Args:
cache_entry: entry which we are movign to the MRU position
- Parameters:
cache_entry (DoublyLinkedListNode) β
- Return type:
None
- pytudes._2021.leetcode.medium._146__lru_cache._remove_from_lru_list(cache_entry)[source]ο
Removes a given node from its doubly-linked list
Precondition: cache_entryβs prev and next attributes are DoublyLinkedListNode instances
Module-level function following guidance from https://google.github.io/styleguide/pyguide.html#217-function-and-method-decorators Namely:
βNever use staticmethod unless forced to in order to integrate with an API defined in an existing library. Write a module level function instead.β
- Args:
cache_entry: cache entry which we must excise from the LRU list
- Parameters:
cache_entry (DoublyLinkedListNode) β
- Return type:
None