Cache that evicts the stalest key
Write a class LRUCache that stores at most capacity key-value pairs and follows the least-recently-used (LRU) policy.
get(key)returns the value stored forkey, or-1if the key is absent.put(key, value)stores the pair, overwriting any old value. If the cache then holds more thancapacitykeys, it evicts the key that has gone longest without being used.
A put, and a get that finds its key, both count as a use. Both operations must be O(1) on average. Keys and values are non-negative integers and capacity >= 1.
The tests call the provided run_cache(capacity, ops): it builds the cache, applies each operation in order and returns the list of results (None for put, shown as null).
Examples
run_cache(2, [["put",1,10],["put",2,20],["get",1],["put",3,30],["get",2],["get",3]])→[null,null,10,null,-1,30]run_cache(2, [["put",1,1],["put",2,2],["put",1,100],["put",3,3],["get",1],["get",2]])→[null,null,null,null,100,-1]run_cache(1, [["put",5,50],["get",5],["put",6,60],["get",5],["get",6]])→[null,50,null,-1,60]