Problem: LRU Cache
Explore how to implement an LRU Cache that supports constant time get and put operations by combining a hash map with a doubly linked list. Understand managing cache capacity, eviction of the least recently used items, and the data structures required for efficient retrieval and updates.
We'll cover the following...
Statement
Design a data structure that follows the constraints of a least recently used (LRU) cache.
Implement the LRUCache class with the following operations:
LRUCache(int capacity): Initializes the LRU cache with a positive sizecapacity.int get(int key): Returns the value associated withkeyif it exists in the cache. Otherwise, returns. void put(int key, int value): Updates the value ofkeyif it already exists in the cache. Otherwise, inserts thekey\-valuepair into the cache. If this insertion causes the number of keys to exceedcapacity, the least recently used key must be evicted.
Note: Both
getandputmust operate inaverage time complexity.
Constraints:
capacitykey...