|
| 1 | +/** |
| 2 | + * Title: LFUCache |
| 3 | + * Description: Design and implement a data structure for a Least Frequently Used (LFU) cache. |
| 4 | + * Author: Hasibul Islam |
| 5 | + * Date: 06/04/2023 |
| 6 | + */ |
| 7 | + |
| 8 | +/** |
| 9 | + * @param {number} capacity |
| 10 | + */ |
| 11 | +var LFUCache = function (capacity) { |
| 12 | + this.key2countRef = new Map(); |
| 13 | + this.LRUCaches = new Map(); |
| 14 | + this.maxSize = capacity; |
| 15 | + this.size = 0; |
| 16 | +}; |
| 17 | + |
| 18 | +/** |
| 19 | + * @param {number} key |
| 20 | + * @return {number} |
| 21 | + */ |
| 22 | +LFUCache.prototype.get = function (key) { |
| 23 | + if (this.maxSize === 0) { |
| 24 | + return -1; |
| 25 | + } |
| 26 | + |
| 27 | + const freq = this.key2countRef.get(key); |
| 28 | + |
| 29 | + if (freq === undefined) { |
| 30 | + return -1; |
| 31 | + } |
| 32 | + |
| 33 | + const prevLRUCache = this.LRUCaches.get(freq); |
| 34 | + const value = prevLRUCache.get(key); |
| 35 | + prevLRUCache.delete(key); |
| 36 | + this.key2countRef.delete(key); |
| 37 | + |
| 38 | + const nextFreq = freq + 1; |
| 39 | + |
| 40 | + const nextLRUCache = this.LRUCaches.get(nextFreq); |
| 41 | + this.key2countRef.set(key, nextFreq); |
| 42 | + if (nextLRUCache === undefined) { |
| 43 | + this.LRUCaches.set(nextFreq, new Map([[key, value]])); |
| 44 | + } else { |
| 45 | + nextLRUCache.set(key, value); |
| 46 | + } |
| 47 | + |
| 48 | + return value; |
| 49 | +}; |
| 50 | + |
| 51 | +/** |
| 52 | + * @param {number} key |
| 53 | + * @param {number} value |
| 54 | + * @return {void} |
| 55 | + */ |
| 56 | +LFUCache.prototype.put = function (key, value) { |
| 57 | + if (this.maxSize === 0) { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + const freq = this.key2countRef.get(key); |
| 62 | + const nextFreq = (freq || 0) + 1; |
| 63 | + |
| 64 | + if (freq) { |
| 65 | + const prevLRUCache = this.LRUCaches.get(freq); |
| 66 | + prevLRUCache.delete(key); |
| 67 | + |
| 68 | + const nextLRUCaches = this.LRUCaches.get(nextFreq); |
| 69 | + this.key2countRef.set(key, nextFreq); |
| 70 | + |
| 71 | + if (nextLRUCaches === undefined) { |
| 72 | + this.LRUCaches.set(nextFreq, new Map([[key, value]])); |
| 73 | + } else { |
| 74 | + nextLRUCaches.set(key, value); |
| 75 | + } |
| 76 | + } else { |
| 77 | + this.size++; |
| 78 | + |
| 79 | + if (this.size > this.maxSize) { |
| 80 | + let LRUCacheForRemove = null; |
| 81 | + |
| 82 | + for (let [curFreq, freqMap] of this.LRUCaches) { |
| 83 | + if (freqMap.size) { |
| 84 | + LRUCacheForRemove = freqMap; |
| 85 | + break; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + const keyForRemoval = LRUCacheForRemove.keys().next().value; |
| 90 | + LRUCacheForRemove.delete(keyForRemoval); |
| 91 | + this.key2countRef.delete(keyForRemoval); |
| 92 | + this.size--; |
| 93 | + } |
| 94 | + |
| 95 | + this.key2countRef.set(key, 1); |
| 96 | + const prevLRUCache = this.LRUCaches.get(nextFreq); |
| 97 | + if (prevLRUCache !== undefined) { |
| 98 | + prevLRUCache.set(key, value); |
| 99 | + } else { |
| 100 | + this.LRUCaches.set(nextFreq, new Map([[key, value]])); |
| 101 | + } |
| 102 | + } |
| 103 | +}; |
| 104 | + |
| 105 | +/** |
| 106 | + * Your LFUCache object will be instantiated and called as such: |
| 107 | + * var obj = new LFUCache(capacity) |
| 108 | + * var param_1 = obj.get(key) |
| 109 | + * obj.put(key,value) |
| 110 | + */ |
0 commit comments