|
| 1 | +## 460. LFU Cache |
| 2 | + |
| 3 | +### Question |
| 4 | +Design and implement a data structure for Least Frequently Used (LFU) cache. It should support the following operations: get and put. |
| 5 | + |
| 6 | +get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1. |
| 7 | +put(key, value) - Set or insert the value if the key is not already present. When the cache reaches its capacity, it should invalidate the least frequently used item before inserting a new item. For the purpose of this problem, when there is a tie (i.e., two or more keys that have the same frequency), the least recently used key would be evicted. |
| 8 | + |
| 9 | +Follow up: |
| 10 | +* Could you do both operations in O(1) time complexity? |
| 11 | + |
| 12 | +``` |
| 13 | +Example: |
| 14 | +
|
| 15 | +LFUCache cache = new LFUCache( 2 /* capacity */ ); |
| 16 | +
|
| 17 | +cache.put(1, 1); |
| 18 | +cache.put(2, 2); |
| 19 | +cache.get(1); // returns 1 |
| 20 | +cache.put(3, 3); // evicts key 2 |
| 21 | +cache.get(2); // returns -1 (not found) |
| 22 | +cache.get(3); // returns 3. |
| 23 | +cache.put(4, 4); // evicts key 1. |
| 24 | +cache.get(1); // returns -1 (not found) |
| 25 | +cache.get(3); // returns 3 |
| 26 | +cache.get(4); // returns 4 |
| 27 | +``` |
| 28 | + |
| 29 | +### Solutions: |
| 30 | +* Method 1: HashMap + PriorityQueue Both get and put are O(NlgN) |
| 31 | + * We create a class node, which saves the freq, tick, key and val. |
| 32 | + * We have a hashMap to save the key and Node. |
| 33 | + * We create a priorityQueue to to save the nodes, the comparator of the priorityQueue is |
| 34 | + 1. if freq is not same, compare the freq. |
| 35 | + 2. if freq is the same, we compare the tick. |
| 36 | + ```Java |
| 37 | + class LFUCache { |
| 38 | + private static final class Node{ |
| 39 | + int key, val; |
| 40 | + Node pre, next; |
| 41 | + int freq, tick; |
| 42 | + public Node(int key, int val, int freq, int tick){ |
| 43 | + this.key = key; |
| 44 | + this.val = val; |
| 45 | + this.freq = freq; |
| 46 | + this.tick = tick; |
| 47 | + } |
| 48 | + } |
| 49 | + private Map<Integer, Node> map; |
| 50 | + private PriorityQueue<Node> pq; |
| 51 | + private int capacity; |
| 52 | + private int size; |
| 53 | + private int tick; |
| 54 | + public LFUCache(int capacity) { |
| 55 | + this.map = new HashMap<>(); |
| 56 | + this.capacity = capacity; |
| 57 | + pq = new PriorityQueue<Node>((n1, n2)->{ |
| 58 | + return n1.freq == n2.freq ? n1.tick - n2.tick: n1.freq - n2.freq; |
| 59 | + }); |
| 60 | + } |
| 61 | + public int get(int key) { |
| 62 | + if(!map.containsKey(key) || capacity == 0) return -1; |
| 63 | + Node node = map.get(key); |
| 64 | + pq.remove(node); |
| 65 | + node.freq++; |
| 66 | + node.tick = tick++; |
| 67 | + pq.offer(node); |
| 68 | + return node.val; |
| 69 | + } |
| 70 | + public void put(int key, int value) { |
| 71 | + if(capacity == 0) return; |
| 72 | + Node node = null; |
| 73 | + if(map.containsKey(key)){ |
| 74 | + node = map.get(key); |
| 75 | + node.val = value; |
| 76 | + pq.remove(node); |
| 77 | + map.remove(key); |
| 78 | + size--; |
| 79 | + }else{ |
| 80 | + node = new Node(key, value, 0, tick); |
| 81 | + } |
| 82 | + node.freq++; |
| 83 | + node.tick = tick++; |
| 84 | + map.put(key, node); |
| 85 | + if(size < capacity){ |
| 86 | + pq.offer(node); |
| 87 | + size++; |
| 88 | + }else{ |
| 89 | + Node lfu = pq.poll(); |
| 90 | + map.remove(lfu.key); |
| 91 | + pq.offer(node); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + /** |
| 96 | + * Your LFUCache object will be instantiated and called as such: |
| 97 | + * LFUCache obj = new LFUCache(capacity); |
| 98 | + * int param_1 = obj.get(key); |
| 99 | + * obj.put(key,value); |
| 100 | + */ |
| 101 | + ``` |
| 102 | + |
| 103 | + * Method 2: Two hashMap + doubleLinkedList O(1) |
| 104 | + ```Java |
| 105 | + class LFUCache { |
| 106 | + private static final class Node{ |
| 107 | + int key, val, freq; |
| 108 | + Node pre, next; |
| 109 | + public Node(int key, int val){ |
| 110 | + this.key = key; |
| 111 | + this.val = val; |
| 112 | + this.freq = 1; |
| 113 | + } |
| 114 | + } |
| 115 | + private static final class DLLList{ |
| 116 | + Node head, tail; |
| 117 | + int size; |
| 118 | + DLLList(){ |
| 119 | + head = new Node(0, 0); |
| 120 | + tail = new Node(0, 0); |
| 121 | + head.next = tail; |
| 122 | + tail.pre = head; |
| 123 | + } |
| 124 | + |
| 125 | + void add(Node node){ |
| 126 | + head.next.pre = node; |
| 127 | + node.next = head.next; |
| 128 | + node.pre = head; |
| 129 | + head.next = node; |
| 130 | + size++; |
| 131 | + } |
| 132 | + void remove(Node node){ |
| 133 | + node.pre.next = node.next; |
| 134 | + node.next.pre = node.pre; |
| 135 | + size--; |
| 136 | + } |
| 137 | + Node removeLast(){ |
| 138 | + if(size > 0){ |
| 139 | + Node node = tail.pre; |
| 140 | + remove(node); |
| 141 | + return node; |
| 142 | + } |
| 143 | + return null; |
| 144 | + } |
| 145 | + } |
| 146 | + private Map<Integer, Node> map; |
| 147 | + private Map<Integer, DLLList> countMap; |
| 148 | + private int size; |
| 149 | + private int capacity; |
| 150 | + private int min; |
| 151 | + public LFUCache(int capacity) { |
| 152 | + this.capacity = capacity; |
| 153 | + this.map = new HashMap<>(); |
| 154 | + this.countMap = new HashMap<>(); |
| 155 | + } |
| 156 | + |
| 157 | + public int get(int key) { |
| 158 | + if(!map.containsKey(key) || size == 0) return -1; |
| 159 | + Node node = map.get(key); |
| 160 | + update(node); |
| 161 | + return node.val; |
| 162 | + } |
| 163 | + |
| 164 | + public void put(int key, int value) { |
| 165 | + if(capacity == 0) return; |
| 166 | + Node node = null; |
| 167 | + if(map.containsKey(key)){ |
| 168 | + node = map.get(key); |
| 169 | + node.val = value; |
| 170 | + update(node); |
| 171 | + }else{ |
| 172 | + node = new Node(key, value); |
| 173 | + map.put(key, node); |
| 174 | + if(size == capacity){ |
| 175 | + DLLList lastList = countMap.get(min); |
| 176 | + map.remove(lastList.removeLast().key); |
| 177 | + size--; |
| 178 | + } |
| 179 | + size++; |
| 180 | + min = 1; |
| 181 | + DLLList newList = countMap.getOrDefault(node.freq, new DLLList()); |
| 182 | + newList.add(node); |
| 183 | + countMap.put(node.freq, newList); |
| 184 | + } |
| 185 | + } |
| 186 | + private void update(Node node){ |
| 187 | + DLLList oldList = countMap.get(node.freq); |
| 188 | + oldList.remove(node); |
| 189 | + if(node.freq == min && oldList.size == 0) min++; |
| 190 | + node.freq++; |
| 191 | + DLLList newList = countMap.getOrDefault(node.freq, new DLLList()); |
| 192 | + newList.add(node); |
| 193 | + countMap.put(node.freq, newList); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * Your LFUCache object will be instantiated and called as such: |
| 199 | + * LFUCache obj = new LFUCache(capacity); |
| 200 | + * int param_1 = obj.get(key); |
| 201 | + * obj.put(key,value); |
| 202 | + */ |
| 203 | + ``` |
| 204 | + |
| 205 | +### Reference |
| 206 | +1. [花花酱 LeetCode 460. LFU Cache](http://zxi.mytechroad.com/blog/hashtable/leetcode-460-lfu-cache/) |
0 commit comments