Post

146. LRU Cache

url:https://leetcode.com/problems/lru-cache/description/

Example


Input:
[“LRUCache”, “put”, “put”, “get”, “put”, “get”, “put”, “get”, “get”, “get”]
[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]
Output:
[null, null, null, 1, null, -1, null, -1, 3, 4]

💻 Code

Approach :

  • Doubly Linked List helps organizing the order for cache (recently used cache)
    • keep update cache and node connection when removing & adding
  • Set dummy nodes for head & tail

  • TC : $O(1)$
  • SC: $O(n)$
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class LRUCache:
    class Node:
        def __init__(self, key, value):
            self.key = key
            self.value = value
            self.prev = None
            self.next = None
    
    def __init__(self, capacity: int):
        self.capacity = capacity
        self.cache = {}
        # head & tail only indicates the first and last node; won't use them
        self.head = self.Node(-1, -1)
        self.tail = self.Node(-1, -1)
        # initial state: head <-> tail
        self.head.next = self.tail
        self.tail.prev = self.head

    def get(self, key: int) -> int:
        if key in self.cache:
            node = self.cache[key]
            # remove the node
            self._remove(node)
            # bring the node to next of the head
            self._add(node)
            return node.value
        return -1
    
    def put(self, key: int, value: int) -> None:
        # if there already? simply remove and put it again
        if key in self.cache:
            self._remove(self.cache[key])
        
        node = self.Node(key, value)
        self._add(node)
        # cache = {key: (key, value)}
        self.cache[key] = node

        # exceeded the capacity?
        # remove the last node (old node)
        if len(self.cache) > self.capacity:
            node_to_remove = self.tail.prev
            self._remove(node_to_remove)
            del self.cache[node_to_remove.key]
        
    def _remove(self, node):
        node.prev.next = node.next
        node.next.prev = node.prev
    
    def _add(self, node):
        node.prev = self.head
        node.next = self.head.next
        self.head.next.prev = node
        self.head.next = node

Additional Practice


460. LFU Cache (hard)

This post is licensed under CC BY 4.0 by the author.