Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put.
get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
put(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.
Could you do both operations in O(1) time complexity?
LRUCache cache = new LRUCache( 2 /_ capacity _/ );
Constructs an empty LinkedHashMap instance with the specified initial capacity, load factor and ordering mode.
initialCapacity - the initial capacity
loadFactor - the load factor, loadFactor is a metric that determines when to increase the size of the LinkedHashMap automatically. By default, this value is 0.75 which means that the size of the map is increased when the map is 75% full.
accessOrder - the ordering mode - true for access-order, false for insertion-order
IllegalArgumentException - if the initial capacity is negative or the load factor is nonpositive