Design a data structure that follows the constraints of a Least Recently Used (LRU) cache.
Implement the LRUCache class:
LRUCache(capacity: int) — initializes the cache with a positive capacity
get(key: int) -> int — returns the value if the key exists, otherwise -1. Accessing a key marks it as recently used.
put(key: int, value: int) -> None — inserts or updates the key. If inserting causes the cache to exceed capacity, evict the least recently used key first.
Both get and put must run in O(1) average time.
Example:
Text
cache = LRUCache(2)
cache.put(1, 1)
cache.put(2, 2)
cache.get(1) # returns 1
cache.put(3, 3) # evicts key 2
cache.get(2) # returns -1