|
| 1 | +/** |
| 2 | + * 460. LFU Cache |
| 3 | + * https://leetcode.com/problems/lfu-cache/ |
| 4 | + * Difficulty: Hard |
| 5 | + * |
| 6 | + * Design and implement a data structure for a Least Frequently Used (LFU) cache. |
| 7 | + * |
| 8 | + * Implement the LFUCache class: |
| 9 | + * - LFUCache(int capacity) Initializes the object with the capacity of the data structure. |
| 10 | + * - int get(int key) Gets the value of the key if the key exists in the cache. Otherwise, |
| 11 | + * returns -1. |
| 12 | + * - void put(int key, int value) Update the value of the key if present, or inserts the |
| 13 | + * key if not already present. When the cache reaches its capacity, it should invalidate |
| 14 | + * and remove the least frequently used key before inserting a new item. For this problem, |
| 15 | + * when there is a tie (i.e., two or more keys with the same frequency), the least recently |
| 16 | + * used key would be invalidated. |
| 17 | + * |
| 18 | + * To determine the least frequently used key, a use counter is maintained for each key in |
| 19 | + * the cache. The key with the smallest use counter is the least frequently used key. |
| 20 | + * |
| 21 | + * When a key is first inserted into the cache, its use counter is set to 1 (due to the |
| 22 | + * put operation). The use counter for a key in the cache is incremented either a get or |
| 23 | + * put operation is called on it. |
| 24 | + * |
| 25 | + * The functions get and put must each run in O(1) average time complexity. |
| 26 | + */ |
| 27 | + |
| 28 | +/** |
| 29 | + * @param {number} capacity |
| 30 | + */ |
| 31 | +var LFUCache = function(capacity) { |
| 32 | + this.capacity = capacity; |
| 33 | + this.size = 0; |
| 34 | + this.minFreq = 0; |
| 35 | + this.values = new Map(); |
| 36 | + this.freq = new Map(); |
| 37 | + this.keys = new Map(); |
| 38 | +}; |
| 39 | + |
| 40 | +/** |
| 41 | + * @param {number} key |
| 42 | + * @return {number} |
| 43 | + */ |
| 44 | +LFUCache.prototype.get = function(key) { |
| 45 | + if (!this.values.has(key)) return -1; |
| 46 | + |
| 47 | + const freq = this.freq.get(key); |
| 48 | + this.freq.set(key, freq + 1); |
| 49 | + |
| 50 | + this.keys.get(freq).delete(key); |
| 51 | + if (!this.keys.get(freq).size) { |
| 52 | + this.keys.delete(freq); |
| 53 | + if (this.minFreq === freq) this.minFreq++; |
| 54 | + } |
| 55 | + |
| 56 | + if (!this.keys.has(freq + 1)) this.keys.set(freq + 1, new Set()); |
| 57 | + this.keys.get(freq + 1).add(key); |
| 58 | + |
| 59 | + return this.values.get(key); |
| 60 | +}; |
| 61 | + |
| 62 | +/** |
| 63 | + * @param {number} key |
| 64 | + * @param {number} value |
| 65 | + * @return {void} |
| 66 | + */ |
| 67 | +LFUCache.prototype.put = function(key, value) { |
| 68 | + if (this.capacity === 0) return; |
| 69 | + |
| 70 | + if (this.values.has(key)) { |
| 71 | + this.values.set(key, value); |
| 72 | + this.get(key); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + if (this.size === this.capacity) { |
| 77 | + const keyToRemove = this.keys.get(this.minFreq).values().next().value; |
| 78 | + this.keys.get(this.minFreq).delete(keyToRemove); |
| 79 | + if (!this.keys.get(this.minFreq).size) { |
| 80 | + this.keys.delete(this.minFreq); |
| 81 | + } |
| 82 | + this.values.delete(keyToRemove); |
| 83 | + this.freq.delete(keyToRemove); |
| 84 | + this.size--; |
| 85 | + } |
| 86 | + |
| 87 | + this.values.set(key, value); |
| 88 | + this.freq.set(key, 1); |
| 89 | + if (!this.keys.has(1)) this.keys.set(1, new Set()); |
| 90 | + this.keys.get(1).add(key); |
| 91 | + this.minFreq = 1; |
| 92 | + this.size++; |
| 93 | +}; |
0 commit comments