|
| 1 | +/* |
| 2 | +
|
| 3 | +
|
| 4 | +-* 460. LFU Cache*- |
| 5 | +
|
| 6 | +
|
| 7 | +Design and implement a data structure for a Least Frequently Used (LFU) cache. |
| 8 | +
|
| 9 | +Implement the LFUCache class: |
| 10 | +
|
| 11 | +LFUCache(int capacity) Initializes the object with the capacity of the data structure. |
| 12 | +int get(int key) Gets the value of the key if the key exists in the cache. Otherwise, returns -1. |
| 13 | +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. |
| 14 | +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. |
| 15 | +
|
| 16 | +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. |
| 17 | +
|
| 18 | +The functions get and put must each run in O(1) average time complexity. |
| 19 | +
|
| 20 | + |
| 21 | +
|
| 22 | +Example 1: |
| 23 | +
|
| 24 | +Input |
| 25 | +["LFUCache", "put", "put", "get", "put", "get", "get", "put", "get", "get", "get"] |
| 26 | +[[2], [1, 1], [2, 2], [1], [3, 3], [2], [3], [4, 4], [1], [3], [4]] |
| 27 | +Output |
| 28 | +[null, null, null, 1, null, -1, 3, null, -1, 3, 4] |
| 29 | +
|
| 30 | +Explanation |
| 31 | +// cnt(x) = the use counter for key x |
| 32 | +// cache=[] will show the last used order for tiebreakers (leftmost element is most recent) |
| 33 | +LFUCache lfu = new LFUCache(2); |
| 34 | +lfu.put(1, 1); // cache=[1,_], cnt(1)=1 |
| 35 | +lfu.put(2, 2); // cache=[2,1], cnt(2)=1, cnt(1)=1 |
| 36 | +lfu.get(1); // return 1 |
| 37 | + // cache=[1,2], cnt(2)=1, cnt(1)=2 |
| 38 | +lfu.put(3, 3); // 2 is the LFU key because cnt(2)=1 is the smallest, invalidate 2. |
| 39 | + // cache=[3,1], cnt(3)=1, cnt(1)=2 |
| 40 | +lfu.get(2); // return -1 (not found) |
| 41 | +lfu.get(3); // return 3 |
| 42 | + // cache=[3,1], cnt(3)=2, cnt(1)=2 |
| 43 | +lfu.put(4, 4); // Both 1 and 3 have the same cnt, but 1 is LRU, invalidate 1. |
| 44 | + // cache=[4,3], cnt(4)=1, cnt(3)=2 |
| 45 | +lfu.get(1); // return -1 (not found) |
| 46 | +lfu.get(3); // return 3 |
| 47 | + // cache=[3,4], cnt(4)=1, cnt(3)=3 |
| 48 | +lfu.get(4); // return 4 |
| 49 | + // cache=[4,3], cnt(4)=2, cnt(3)=3 |
| 50 | + |
| 51 | +
|
| 52 | +Constraints: |
| 53 | +
|
| 54 | +0 <= capacity <= 104 |
| 55 | +0 <= key <= 105 |
| 56 | +0 <= value <= 109 |
| 57 | +At most 2 * 105 calls will be made to get and put.Design and implement a data structure for a Least Frequently Used (LFU) cache. |
| 58 | +
|
| 59 | +Implement the LFUCache class: |
| 60 | +
|
| 61 | +LFUCache(int capacity) Initializes the object with the capacity of the data structure. |
| 62 | +int get(int key) Gets the value of the key if the key exists in the cache. Otherwise, returns -1. |
| 63 | +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. |
| 64 | +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. |
| 65 | +
|
| 66 | +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. |
| 67 | +
|
| 68 | +The functions get and put must each run in O(1) average time complexity. |
| 69 | +
|
| 70 | + |
| 71 | +
|
| 72 | +Example 1: |
| 73 | +
|
| 74 | +Input |
| 75 | +["LFUCache", "put", "put", "get", "put", "get", "get", "put", "get", "get", "get"] |
| 76 | +[[2], [1, 1], [2, 2], [1], [3, 3], [2], [3], [4, 4], [1], [3], [4]] |
| 77 | +Output |
| 78 | +[null, null, null, 1, null, -1, 3, null, -1, 3, 4] |
| 79 | +
|
| 80 | +Explanation |
| 81 | +// cnt(x) = the use counter for key x |
| 82 | +// cache=[] will show the last used order for tiebreakers (leftmost element is most recent) |
| 83 | +LFUCache lfu = new LFUCache(2); |
| 84 | +lfu.put(1, 1); // cache=[1,_], cnt(1)=1 |
| 85 | +lfu.put(2, 2); // cache=[2,1], cnt(2)=1, cnt(1)=1 |
| 86 | +lfu.get(1); // return 1 |
| 87 | + // cache=[1,2], cnt(2)=1, cnt(1)=2 |
| 88 | +lfu.put(3, 3); // 2 is the LFU key because cnt(2)=1 is the smallest, invalidate 2. |
| 89 | + // cache=[3,1], cnt(3)=1, cnt(1)=2 |
| 90 | +lfu.get(2); // return -1 (not found) |
| 91 | +lfu.get(3); // return 3 |
| 92 | + // cache=[3,1], cnt(3)=2, cnt(1)=2 |
| 93 | +lfu.put(4, 4); // Both 1 and 3 have the same cnt, but 1 is LRU, invalidate 1. |
| 94 | + // cache=[4,3], cnt(4)=1, cnt(3)=2 |
| 95 | +lfu.get(1); // return -1 (not found) |
| 96 | +lfu.get(3); // return 3 |
| 97 | + // cache=[3,4], cnt(4)=1, cnt(3)=3 |
| 98 | +lfu.get(4); // return 4 |
| 99 | + // cache=[4,3], cnt(4)=2, cnt(3)=3 |
| 100 | + |
| 101 | +
|
| 102 | +Constraints: |
| 103 | +
|
| 104 | +0 <= capacity <= 104 |
| 105 | +0 <= key <= 105 |
| 106 | +0 <= value <= 109 |
| 107 | +At most 2 * 105 calls will be made to get and put. |
| 108 | +
|
| 109 | +*/ |
| 110 | + |
| 111 | +/* |
| 112 | +
|
| 113 | +
|
| 114 | +class LFUCache { |
| 115 | +
|
| 116 | + LFUCache(int capacity) { |
| 117 | +
|
| 118 | + } |
| 119 | + |
| 120 | + int get(int key) { |
| 121 | +
|
| 122 | + } |
| 123 | + |
| 124 | + void put(int key, int value) { |
| 125 | +
|
| 126 | + } |
| 127 | +} |
| 128 | +
|
| 129 | +
|
| 130 | +*/ |
| 131 | + |
| 132 | +// class LFUCache { |
| 133 | +// late int capacity; |
| 134 | +// int minFreq = 0; |
| 135 | +// HashMap<int, int> keyToVal = HashMap(); |
| 136 | +// HashMap<int, int> keyToFreq = HashMap(); |
| 137 | +// HashMap<int, LinkedHashSet<int>> freqToLRUKeys = HashMap(); |
| 138 | +// LFUCache(int capacity) { |
| 139 | +// this.capacity = capacity; |
| 140 | +// } |
| 141 | + |
| 142 | +// int get(int key) { |
| 143 | +// if (!keyToVal.containsKey(key)) return -1; |
| 144 | + |
| 145 | +// final int freq = keyToFreq[key] ?? 0; |
| 146 | +// freqToLRUKeys[freq]?.remove(key); |
| 147 | +// if (freq == minFreq && freqToLRUKeys[freq]!.isEmpty) { |
| 148 | +// freqToLRUKeys.remove(freq); |
| 149 | +// ++minFreq; |
| 150 | +// } |
| 151 | + |
| 152 | +// // Increase key's freq by 1 |
| 153 | +// // Add this key to next freq's list |
| 154 | +// putFreq(key, freq + 1); |
| 155 | +// return keyToVal[key] ?? 0; |
| 156 | +// } |
| 157 | + |
| 158 | +// void put(int key, int value) { |
| 159 | +// if (capacity == 0) return; |
| 160 | +// if (keyToVal.containsKey(key)) { |
| 161 | +// keyToVal[key] = value; |
| 162 | +// get(key); // Update key's count |
| 163 | +// return; |
| 164 | +// } |
| 165 | + |
| 166 | +// if (keyToVal.length == capacity) { |
| 167 | +// // Evict LRU key from the minFreq list |
| 168 | +// final int keyToEvict = freqToLRUKeys[minFreq]...iterator.current; |
| 169 | +// freqToLRUKeys[minFreq]?.remove(keyToEvict); |
| 170 | +// keyToVal.remove(keyToEvict); |
| 171 | +// } |
| 172 | + |
| 173 | +// minFreq = 1; |
| 174 | +// putFreq(key, minFreq); // Add new key and freq |
| 175 | +// keyToVal[key] = value; // Add new key and value |
| 176 | +// } |
| 177 | + |
| 178 | +// void putFreq(int key, int freq) { |
| 179 | +// keyToFreq[key] = freq; |
| 180 | +// freqToLRUKeys.putIfAbsent(freq, () => LinkedHashSet()); |
| 181 | +// freqToLRUKeys[freq]?.add(key); |
| 182 | +// } |
| 183 | +// } |
| 184 | + |
| 185 | +// import 'dart:collection'; |
| 186 | + |
| 187 | +// class Node { |
| 188 | +// late int key; |
| 189 | +// late int val; |
| 190 | +// late Node next; |
| 191 | +// late Node prev; |
| 192 | +// int freq = 1; |
| 193 | +// Node(int k, int v) { |
| 194 | +// key = k; |
| 195 | +// val = v; |
| 196 | +// } |
| 197 | +// } |
| 198 | + |
| 199 | +// class DoublyLinkedList { |
| 200 | +// late Node head; |
| 201 | +// late Node tail; |
| 202 | +// DoublyLinkedList() { |
| 203 | +// head = Node(-1, -1); |
| 204 | +// tail = Node(-1, -1); |
| 205 | +// head.next = tail; |
| 206 | +// tail.prev = head; |
| 207 | +// } |
| 208 | +// void addNode(Node v) { |
| 209 | +// Node next = head.next; |
| 210 | +// head.next = v; |
| 211 | +// v.prev = head; |
| 212 | +// head.next = v; |
| 213 | +// v.next = next; |
| 214 | +// next.prev = v; |
| 215 | +// } |
| 216 | + |
| 217 | +// Node removeNode() { |
| 218 | +// Node node = tail.prev; |
| 219 | +// node.prev.next = tail; |
| 220 | +// tail.prev = node.prev; |
| 221 | +// return node; |
| 222 | +// } |
| 223 | + |
| 224 | +// Node removeNodeAt(Node v) { |
| 225 | +// Node prev = v.prev; |
| 226 | +// Node next = v.next; |
| 227 | +// prev.next = next; |
| 228 | +// next.prev = prev; |
| 229 | +// return v; |
| 230 | +// } |
| 231 | + |
| 232 | +// bool isEmpty() { |
| 233 | +// if (head.next == tail) return true; |
| 234 | +// return false; |
| 235 | +// } |
| 236 | +// } |
| 237 | + |
| 238 | +// class LFUCache { |
| 239 | +// HashMap<int, DoublyLinkedList> freqList = HashMap<int, DoublyLinkedList>(); |
| 240 | +// HashMap<int, Node> lfuCache = HashMap<int, Node>(); |
| 241 | +// late int capacity; |
| 242 | +// late int minFreq; |
| 243 | +// LFUCache(int capacity) { |
| 244 | +// this.capacity = capacity; |
| 245 | +// minFreq = 1; |
| 246 | +// } |
| 247 | + |
| 248 | +// int get(int key) { |
| 249 | +// if (lfuCache[key] == null) return -1; |
| 250 | +// Node v = lfuCache[key] ?? 0 as Node; |
| 251 | +// freqList[v.freq]?.removeNodeAt(v); |
| 252 | +// if (freqList[v.freq]!.isEmpty()) { |
| 253 | +// if (minFreq == v.freq) { |
| 254 | +// minFreq = v.freq + 1; |
| 255 | +// } |
| 256 | +// } |
| 257 | +// v.freq += 1; |
| 258 | +// if (freqList[v.freq] == null) { |
| 259 | +// DoublyLinkedList d = new DoublyLinkedList(); |
| 260 | +// d.addNode(v); |
| 261 | +// freqList[v.freq] = d; |
| 262 | +// } else { |
| 263 | +// freqList[v.freq]?.addNode(v); |
| 264 | +// } |
| 265 | +// return v.val; |
| 266 | +// } |
| 267 | + |
| 268 | +// void put(int key, int value) { |
| 269 | +// if (capacity == 0) return; |
| 270 | +// if (lfuCache[key] != null) { |
| 271 | +// Node v = lfuCache[key] ?? 0 as Node; |
| 272 | +// freqList[v.freq]?.removeNodeAt(v); |
| 273 | +// if (freqList[v.freq]!.isEmpty()) { |
| 274 | +// if (minFreq == v.freq) minFreq = v.freq + 1; |
| 275 | +// } |
| 276 | +// v.freq += 1; |
| 277 | +// if (freqList[v.freq] == null) { |
| 278 | +// DoublyLinkedList d = new DoublyLinkedList(); |
| 279 | +// d.addNode(v); |
| 280 | +// freqList[v.freq] = d; |
| 281 | +// } else { |
| 282 | +// freqList[v.freq]?.addNode(v); |
| 283 | +// } |
| 284 | +// v.val = value; |
| 285 | +// } else { |
| 286 | +// if (lfuCache.length == capacity) { |
| 287 | +// Node v = freqList[minFreq]!.removeNode(); |
| 288 | +// lfuCache.remove(v.key); |
| 289 | +// } |
| 290 | +// Node newNode = new Node(key, value); |
| 291 | +// lfuCache[key] = newNode; |
| 292 | +// if (freqList[1] != null) { |
| 293 | +// freqList[1]?.addNode(newNode); |
| 294 | +// } else { |
| 295 | +// DoublyLinkedList d = new DoublyLinkedList(); |
| 296 | +// d.addNode(newNode); |
| 297 | +// freqList[1] = d; |
| 298 | +// } |
| 299 | +// minFreq = 1; |
| 300 | +// } |
| 301 | +// } |
| 302 | +// } |
| 303 | + |
0 commit comments