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.["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 <= 1000 <= key <= 10000 <= value <= 10000At most 1000 calls will be made to get and putRun your code to see results
Use Cmd+Enter to run