LRU Cache
Advanced Implementation & Design DSA practice problem on Onlearn.
Difficulty: hard.
Topics: Design and Implementation of an LRU (Least Recently Used) Cache with O(1) Operations, Data Structures, Linked List, Doubly Linked List, Node, Hash Map, Time Complexity, Space Complexity, Algorithm, linked list, general programming, hash map, cache replacement policies, data structure integration, time complexity analysis, Cache Design, LRU Cache, Specific Time Complexities.
LRU Cache Design a data structure that follows the constraints of a Least Recently Used (LRU) cache. Implement the LRUCache class: LRUCache(int capacity): Initializes the LRU cache with a positive size capacity. int get(int key): Returns the value of the key if the key exists, otherwise returns 1. void put(int key, int value): Updates the value of the key if the key exists. Otherwise, adds the key value pair to the cache. If the number of keys exceeds the capacity from this operation, evicts the least recently used key. The functions get and put must each run in O(1) average time complexity. Example: Input: ["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"] [[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]] Output: [null, null, null, 1, null, 1, null, 1, 3, 4] Explanation: