|
| 1 | +/* |
| 2 | +
|
| 3 | +-* 146. LRU Cache *- |
| 4 | +
|
| 5 | +
|
| 6 | +Design a data structure that follows the constraints of a Least Recently Used (LRU) cache. |
| 7 | +
|
| 8 | +Implement the LRUCache class: |
| 9 | +
|
| 10 | +LRUCache(int capacity) Initialize the LRU cache with positive size capacity. |
| 11 | +int get(int key) Return the value of the key if the key exists, otherwise return -1. |
| 12 | +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. |
| 13 | +The functions get and put must each run in O(1) average time complexity. |
| 14 | +
|
| 15 | + |
| 16 | +
|
| 17 | +Example 1: |
| 18 | +
|
| 19 | +Input |
| 20 | +["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"] |
| 21 | +[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]] |
| 22 | +Output |
| 23 | +[null, null, null, 1, null, -1, null, -1, 3, 4] |
| 24 | +
|
| 25 | +Explanation |
| 26 | +LRUCache lRUCache = new LRUCache(2); |
| 27 | +lRUCache.put(1, 1); // cache is {1=1} |
| 28 | +lRUCache.put(2, 2); // cache is {1=1, 2=2} |
| 29 | +lRUCache.get(1); // return 1 |
| 30 | +lRUCache.put(3, 3); // LRU key was 2, evicts key 2, cache is {1=1, 3=3} |
| 31 | +lRUCache.get(2); // returns -1 (not found) |
| 32 | +lRUCache.put(4, 4); // LRU key was 1, evicts key 1, cache is {4=4, 3=3} |
| 33 | +lRUCache.get(1); // return -1 (not found) |
| 34 | +lRUCache.get(3); // return 3 |
| 35 | +lRUCache.get(4); // return 4 |
| 36 | + |
| 37 | +
|
| 38 | +Constraints: |
| 39 | +
|
| 40 | +1 <= capacity <= 3000 |
| 41 | +0 <= key <= 104 |
| 42 | +0 <= value <= 105 |
| 43 | +At most 2 * 105 calls will be made to get and put. |
| 44 | +
|
| 45 | +
|
| 46 | +
|
| 47 | +class LRUCache { |
| 48 | +
|
| 49 | + LRUCache(int capacity) { |
| 50 | +
|
| 51 | + } |
| 52 | + |
| 53 | + int get(int key) { |
| 54 | +
|
| 55 | + } |
| 56 | + |
| 57 | + void put(int key, int value) { |
| 58 | +
|
| 59 | + } |
| 60 | +} |
| 61 | +
|
| 62 | +/** |
| 63 | + * Your LRUCache object will be instantiated and called as such: |
| 64 | + * LRUCache obj = LRUCache(capacity); |
| 65 | + * int param1 = obj.get(key); |
| 66 | + * obj.put(key,value); |
| 67 | + */ |
| 68 | +
|
| 69 | +*/ |
| 70 | + |
| 71 | +// class LRUCache { |
| 72 | + |
| 73 | +// LRUCache(int capacity) { |
| 74 | + |
| 75 | +// } |
| 76 | + |
| 77 | +// int get(int key) { |
| 78 | + |
| 79 | +// } |
| 80 | + |
| 81 | +// void put(int key, int value) { |
| 82 | + |
| 83 | +// } |
| 84 | +// } |
| 85 | +// import 'dart:collection'; |
| 86 | + |
| 87 | +// class LRUCache { |
| 88 | +// late int cnt; |
| 89 | +// Map<int, int> mp = {}; |
| 90 | +// Queue<int> dq = Queue(); |
| 91 | +// Set<MapEntry<int, int>> st = {}; |
| 92 | +// Map<int, int> val = {}; |
| 93 | +// int res = 0; |
| 94 | + |
| 95 | +// LRUCache(int capacity) { |
| 96 | +// cnt = capacity; |
| 97 | +// } |
| 98 | + |
| 99 | +// int get(int key) { |
| 100 | +// if (mp.containsKey(key)) { |
| 101 | +// int v = val[key]!; |
| 102 | +// st.remove(MapEntry(v, key)); |
| 103 | +// res++; |
| 104 | +// st.add(MapEntry(res, key)); |
| 105 | +// val[key] = res; |
| 106 | +// return mp[key]!; |
| 107 | +// } |
| 108 | +// return -1; // Return -1 if the key is not present in the cache |
| 109 | +// } |
| 110 | + |
| 111 | +// void put(int key, int value) { |
| 112 | +// if (!mp.containsKey(key)) { |
| 113 | +// if (mp.length == cnt) { |
| 114 | +// int r = dq.removeFirst(); |
| 115 | +// st.remove(MapEntry(val[r]!, r)); |
| 116 | +// mp.remove(r); |
| 117 | +// val.remove(r); |
| 118 | +// } |
| 119 | +// res++; |
| 120 | +// mp[key] = value; |
| 121 | +// val[key] = res; |
| 122 | +// st.add(MapEntry(res, key)); |
| 123 | +// dq.addLast(key); |
| 124 | +// } else { |
| 125 | +// int v = val[key]!; |
| 126 | +// st.remove(MapEntry(v, key)); |
| 127 | +// res++; |
| 128 | +// st.add(MapEntry(res, key)); |
| 129 | +// mp[key] = value; |
| 130 | +// val[key] = res; |
| 131 | +// } |
| 132 | +// } |
| 133 | +// } |
| 134 | + |
| 135 | +import 'dart:collection'; |
| 136 | + |
| 137 | +class LRUCacheOne { |
| 138 | + final Queue<int> leastUsedCache = Queue<int>(); |
| 139 | + final List<int> usages = List<int>.filled(10001, 0); |
| 140 | + final List<int> keyValuePair = List<int>.filled(10001, -1); |
| 141 | + int size = 0; |
| 142 | + int cap = 0; |
| 143 | + |
| 144 | + LRUCacheOne(int capacity) { |
| 145 | + cap = capacity; |
| 146 | + } |
| 147 | + |
| 148 | + int get(int key) { |
| 149 | + if (keyValuePair[key] != -1) { |
| 150 | + leastUsedCache.add(key); |
| 151 | + usages[key]++; |
| 152 | + } |
| 153 | + return keyValuePair[key]; |
| 154 | + } |
| 155 | + |
| 156 | + void put(int key, int value) { |
| 157 | + if (size < cap || keyValuePair[key] != -1) { |
| 158 | + if (keyValuePair[key] == -1) size++; |
| 159 | + leastUsedCache.add(key); |
| 160 | + usages[key]++; |
| 161 | + keyValuePair[key] = value; |
| 162 | + return; |
| 163 | + } |
| 164 | + while (usages[leastUsedCache.first] != 1) { |
| 165 | + usages[leastUsedCache.first]--; |
| 166 | + leastUsedCache.removeFirst(); |
| 167 | + } |
| 168 | + keyValuePair[leastUsedCache.first] = -1; |
| 169 | + usages[leastUsedCache.first]--; |
| 170 | + leastUsedCache.removeFirst(); |
| 171 | + leastUsedCache.add(key); |
| 172 | + usages[key]++; |
| 173 | + keyValuePair[key] = value; |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +class LRUCacheFAST { |
| 178 | + late int k; |
| 179 | + final Map<int, List<int>> mp = Map(); |
| 180 | + final Queue<int> q = Queue<int>(); |
| 181 | + |
| 182 | + LRUCacheFAST(int capacity) { |
| 183 | + k = capacity; |
| 184 | + } |
| 185 | + |
| 186 | + int get(int key) { |
| 187 | + if (mp.containsKey(key)) { |
| 188 | + mp[key]![1]++; |
| 189 | + q.add(key); |
| 190 | + return mp[key]![0]; |
| 191 | + } |
| 192 | + return -1; |
| 193 | + } |
| 194 | + |
| 195 | + void put(int key, int value) { |
| 196 | + if (mp.containsKey(key)) { |
| 197 | + mp[key]![0] = value; |
| 198 | + mp[key]![1]++; |
| 199 | + q.add(key); |
| 200 | + } else { |
| 201 | + mp[key] = [value, 1]; |
| 202 | + q.add(key); |
| 203 | + } |
| 204 | + |
| 205 | + // Check if capacity is exceeded. |
| 206 | + if (mp.length > k) { |
| 207 | + while (q.isNotEmpty) { |
| 208 | + int ky = q.removeFirst(); |
| 209 | + // Decrement the frequency of ky. |
| 210 | + mp[ky]![1]--; |
| 211 | + // If no more key present in the queue, that means it is the least recently used. |
| 212 | + if (mp[ky]![1] == 0) { |
| 213 | + mp.remove(ky); |
| 214 | + break; |
| 215 | + } |
| 216 | + } |
| 217 | + } |
| 218 | + } |
| 219 | +} |
| 220 | + |
| 221 | +// |
| 222 | + |
| 223 | +class Node { |
| 224 | + late final int key; |
| 225 | + late final int val; |
| 226 | + Node? prev = null; |
| 227 | + Node? next = null; |
| 228 | + |
| 229 | + Node(this.key, this.val); |
| 230 | +} |
| 231 | + |
| 232 | +class LRUCache { |
| 233 | + late int cap; |
| 234 | + final Map<int, Node> mp = {}; |
| 235 | + |
| 236 | + final Node head = Node(-1, -1); |
| 237 | + final Node tail = Node(-1, -1); |
| 238 | + |
| 239 | + LRUCache(int capacity) { |
| 240 | + cap = capacity; |
| 241 | + head.next = tail; |
| 242 | + tail.prev = head; |
| 243 | + } |
| 244 | + |
| 245 | + void addNode(Node newNode) { |
| 246 | + Node? temp = head.next; |
| 247 | + newNode.next = temp; |
| 248 | + newNode.prev = head; |
| 249 | + head.next = newNode; |
| 250 | + temp?.prev = newNode; |
| 251 | + } |
| 252 | + |
| 253 | + void deleteNode(Node delNode) { |
| 254 | + Node? delPrev = delNode.prev; |
| 255 | + Node? delNext = delNode.next; |
| 256 | + delPrev?.next = delNext; |
| 257 | + delNext?.prev = delPrev; |
| 258 | + } |
| 259 | + |
| 260 | + int get(int key) { |
| 261 | + if (mp.containsKey(key)) { |
| 262 | + Node resNode = mp[key]!; |
| 263 | + int res = resNode.val; |
| 264 | + deleteNode(resNode); |
| 265 | + addNode(resNode); |
| 266 | + mp[key] = head.next!; |
| 267 | + return res; |
| 268 | + } |
| 269 | + return -1; |
| 270 | + } |
| 271 | + |
| 272 | + void put(int key, int value) { |
| 273 | + if (mp.containsKey(key)) { |
| 274 | + Node existingNode = mp[key]!; |
| 275 | + mp.remove(key); |
| 276 | + deleteNode(existingNode); |
| 277 | + } |
| 278 | + if (mp.length == cap) { |
| 279 | + mp.remove(tail.prev!.key); |
| 280 | + deleteNode(tail.prev!); |
| 281 | + } |
| 282 | + addNode(Node(key, value)); |
| 283 | + mp[key] = head.next!; |
| 284 | + } |
| 285 | +} |
0 commit comments