Problem: LRU Cache
Explore how to implement an efficient Least Recently Used (LRU) cache in C#. Understand the use of a dictionary for quick key lookups combined with a doubly linked list to track usage order, ensuring that both Get and Put operations run in constant time. This lesson helps you design and optimize caches, a crucial skill for handling memory in real-world applications.
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:
public LRUCache(int capacity): Initializes the LRU cache with a positive sizecapacity.public int Get(int key): Returns the value associated withkeyif it exists in the cache. Otherwise, returns. public 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:
capacity...