Design a data structure that follows the constraints of a Least Recently Used (LRU) cache.

Implement the LRUCache class:

  • LRUCache(int capacity) Initialize the LRU cache with positive size capacity.
  • int get(int key) Return the value of the key if the key exists, otherwise return -1.
  • void put(int key, int value) Update the value of the key if the key exists. Otherwise, add the key-value pair to the cache. If the number of keys exceeds the capacity from this operation, evict the least recently used key.

The functions get and put must each run in O(1) average time complexity.

Idea

Use hashmap + counter + deque. (may be also done using linked list)

class LRUCache:
 
    def __init__(self, capacity: int):
        self.dic = {}
        self.counter = defaultdict(int)
        self.d = deque()
        self.capacity = capacity
 
    def get(self, key: int) -> int:
        if key in self.dic:
            self.d.append(key)
            self.counter[key] += 1
            return self.dic.get(key)
        return -1
 
    def put(self, key: int, value: int) -> None:
        if key not in self.dic and len(self.counter) >= self.capacity:
            while True:
                left = self.d.popleft()
                self.counter[left] -= 1
                if not self.counter[left]:
                    del self.counter[left]
                    del self.dic[left]
                    break
 
        self.dic[key] = value
        self.counter[key] += 1
        self.d.append(key)

Approach 2: OrderedDict

class LRUCache:
    def __init__(self, capacity: int):
        self.capacity = capacity
        self.dic = OrderedDict()
 
    def get(self, key: int) -> int:
        if key not in self.dic:
            return -1
 
        self.dic.move_to_end(key)
        return self.dic[key]
 
    def put(self, key: int, value: int) -> None:
        if key in self.dic:
            self.dic.move_to_end(key)
 
        self.dic[key] = value
        if len(self.dic) > self.capacity:
            self.dic.popitem(False)