Problems
MEDIUM
LRU Cache Implementation
+100 ptsLogin to Submit
Data Structures & AlgorithmsCloud ComputingHash TableLinked ListDesign
Problem Description
Design a data structure that follows the constraints of a Least Recently Used (LRU) cache.
Implement the LRUCache class:
- LRUCache(int capacity) Initialize the LRU cache with positive size capacity.
- int get(int key) Return the value of the key if exists, otherwise return -1.
- void put(int key, int value) Update or insert the value. Evict LRU key when capacity exceeded.
Example
Input
LRUCache cache = new LRUCache(2); cache.put(1, 1); cache.put(2, 2); cache.get(1);
Output
1
Constraints
Time: 2s
Memory: 256MB