Google DailyCodingProblem TODO Hard
Problem
Implement an LRU (Least Recently Used) cache. It should be able to be initialized with a cache size n
, and contain the following methods:
set(key, value)
: setskey
tovalue
. If there are alreadyn
items in the cache and we are adding a new item, then it should also remove the least recently used item.get(key)
: gets the value atkey
. If no such key exists, return null.
Each operation should run in O(1) time.