Design a data structure that follows the constraints of a Least Recently Used (LRU) cache.
Implement the LRUCache class:
LRUCache(capacity) — Initialize the LRU cache with positive size capacity.get(key) — Return the value of the key if it exists, otherwise return -1. This also marks the key as recently used.put(key, value) — Update the value of the key if it exists. Otherwise, add the key-value pair to the cache. If the number of keys exceeds the capacity, evict the least recently used key.The functions get and put must each run in O(1) average time complexity.
["LRUCache","put","put","get","put","get","put","get","get","get"]
[[2],[1,1],[2,2],[1],[3,3],[2],[4,4],[1],[3],[4]][null,null,null,1,null,-1,null,-1,3,4]1 <= capacity <= 30000 <= key <= 10^40 <= value <= 10^5At most 2 * 10^5 calls will be made to get and putExpected time complexity: O(1) per operationRun your code to see results
Use Cmd+Enter to run