|
| 1 | +# 460. LFU Cache |
| 2 | +Design and implement a data structure for a [Least Frequently Used (LFU)](https://en.wikipedia.org/wiki/Least_frequently_used) cache. |
| 3 | + |
| 4 | +Implement the `LFUCache` class: |
| 5 | +* `LFUCache(int capacity)` Initializes the object with the `capacity` of the data structure. |
| 6 | +* `int get(int key)` Gets the value of the `key` if the `key` exists in the cache. Otherwise, returns `-1`. |
| 7 | +* `void put(int key, int value)` Update the value of the `key` if present, or inserts the `key` if not already present. When the cache reaches its `capacity`, it should invalidate and remove the **least frequently used** key before inserting a new item. For this problem, when there is a **tie** (i.e., two or more keys with the same frequency), the **least recently used** `key` would be invalidated. |
| 8 | + |
| 9 | +To determine the least frequently used key, a **use counter** is maintained for each key in the cache. The key with the smallest **use counter** is the least frequently used key. |
| 10 | + |
| 11 | +When a key is first inserted into the cache, its **use counter** is set to `1` (due to the `put` operation). The **use counter** for a key in the cache is incremented either a `get` or `put` operation is called on it. |
| 12 | + |
| 13 | +The functions `get` and `put` must each run in `O(1)` average time complexity. |
| 14 | + |
| 15 | +#### Example 1: |
| 16 | +<pre> |
| 17 | +<strong>Input:</strong> |
| 18 | +["LFUCache", "put", "put", "get", "put", "get", "get", "put", "get", "get", "get"] |
| 19 | +[[2], [1, 1], [2, 2], [1], [3, 3], [2], [3], [4, 4], [1], [3], [4]] |
| 20 | +<strong>Output:</strong> |
| 21 | +[null, null, null, 1, null, -1, 3, null, -1, 3, 4] |
| 22 | +<strong>Explanation:</strong> |
| 23 | +// cnt(x) = the use counter for key x |
| 24 | +// cache=[] will show the last used order for tiebreakers (leftmost element is most recent) |
| 25 | +LFUCache lfu = new LFUCache(2); |
| 26 | +lfu.put(1, 1); // cache=[1,_], cnt(1)=1 |
| 27 | +lfu.put(2, 2); // cache=[2,1], cnt(2)=1, cnt(1)=1 |
| 28 | +lfu.get(1); // return 1 |
| 29 | + // cache=[1,2], cnt(2)=1, cnt(1)=2 |
| 30 | +lfu.put(3, 3); // 2 is the LFU key because cnt(2)=1 is the smallest, invalidate 2. |
| 31 | + // cache=[3,1], cnt(3)=1, cnt(1)=2 |
| 32 | +lfu.get(2); // return -1 (not found) |
| 33 | +lfu.get(3); // return 3 |
| 34 | + // cache=[3,1], cnt(3)=2, cnt(1)=2 |
| 35 | +lfu.put(4, 4); // Both 1 and 3 have the same cnt, but 1 is LRU, invalidate 1. |
| 36 | + // cache=[4,3], cnt(4)=1, cnt(3)=2 |
| 37 | +lfu.get(1); // return -1 (not found) |
| 38 | +lfu.get(3); // return 3 |
| 39 | + // cache=[3,4], cnt(4)=1, cnt(3)=3 |
| 40 | +lfu.get(4); // return 4 |
| 41 | + // cache=[4,3], cnt(4)=2, cnt(3)=3 |
| 42 | +</pre> |
| 43 | + |
| 44 | +#### Constraints: |
| 45 | +* <code>1 <= capacity <= 10<sup>4</sup></code> |
| 46 | +* <code>0 <= key <= 10<sup>5</sup></code> |
| 47 | +* <code>0 <= value <= 10<sup>9</sup></code> |
| 48 | +* At most <code>2 * 10<sup>5</sup></code> calls will be made to `get` and `put`. |
| 49 | + |
| 50 | +## Solutions (Python) |
| 51 | + |
| 52 | +### 1. Solution |
| 53 | +```Python |
| 54 | +class ListNode: |
| 55 | + |
| 56 | + def __init__(self, key=-1, val=0, prev=None, next=None): |
| 57 | + self.key = key |
| 58 | + self.val = val |
| 59 | + self.prev = prev |
| 60 | + self.next = next |
| 61 | + self.count = 0 |
| 62 | + |
| 63 | + |
| 64 | +class LFUCache: |
| 65 | + |
| 66 | + def __init__(self, capacity: int): |
| 67 | + self.capacity = capacity |
| 68 | + self.cache = {} |
| 69 | + self.headtails = {} |
| 70 | + self.hair = ListNode() |
| 71 | + |
| 72 | + def get(self, key: int) -> int: |
| 73 | + if key not in self.cache: |
| 74 | + return -1 |
| 75 | + |
| 76 | + node = self.cache[key] |
| 77 | + head, tail = self.headtails[node.count] |
| 78 | + if head.key == node.key and tail.key == node.key: |
| 79 | + self.headtails.pop(node.count) |
| 80 | + elif head.key == node.key: |
| 81 | + self.headtails[node.count][0] = node.next |
| 82 | + elif tail.key == node.key: |
| 83 | + self.headtails[node.count][1] = node.prev |
| 84 | + node.prev.next = node.next |
| 85 | + if node.next is not None: |
| 86 | + node.next.prev = node.prev |
| 87 | + |
| 88 | + node.count += 1 |
| 89 | + if tail.next is None or tail.next.count > node.count: |
| 90 | + if tail.key == node.key: |
| 91 | + tail = node.prev |
| 92 | + self.headtails[node.count] = [node, node] |
| 93 | + else: |
| 94 | + tail = self.headtails[node.count][1] |
| 95 | + self.headtails[node.count][1] = node |
| 96 | + node.prev = tail |
| 97 | + node.next = tail.next |
| 98 | + tail.next = node |
| 99 | + if node.next is not None: |
| 100 | + node.next.prev = node |
| 101 | + |
| 102 | + return node.val |
| 103 | + |
| 104 | + def put(self, key: int, value: int) -> None: |
| 105 | + if key not in self.cache: |
| 106 | + if len(self.cache) == self.capacity: |
| 107 | + head, tail = self.headtails[self.hair.next.count] |
| 108 | + self.cache.pop(head.key) |
| 109 | + if head.key == tail.key: |
| 110 | + self.headtails.pop(head.count) |
| 111 | + else: |
| 112 | + self.headtails[head.count][0] = head.next |
| 113 | + self.hair.next = head.next |
| 114 | + if head.next is not None: |
| 115 | + head.next.prev = self.hair |
| 116 | + |
| 117 | + node = ListNode(key, value, self.hair, self.hair.next) |
| 118 | + self.cache[key] = node |
| 119 | + self.headtails[0] = [node, node] |
| 120 | + self.hair.next = node |
| 121 | + if node.next is not None: |
| 122 | + node.next.prev = node |
| 123 | + |
| 124 | + self.cache[key].val = value |
| 125 | + self.get(key) |
| 126 | + |
| 127 | + |
| 128 | +# Your LFUCache object will be instantiated and called as such: |
| 129 | +# obj = LFUCache(capacity) |
| 130 | +# param_1 = obj.get(key) |
| 131 | +# obj.put(key,value) |
| 132 | +``` |
0 commit comments