|
| 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 | +classLFUCache { |
| 38 | +privatestaticfinalclassNode{ |
| 39 | +int key, val; |
| 40 | +Node pre, next; |
| 41 | +int freq, tick; |
| 42 | +publicNode(intkey,intval,intfreq,inttick){ |
| 43 | +this.key= key; |
| 44 | +this.val= val; |
| 45 | +this.freq= freq; |
| 46 | +this.tick= tick; |
| 47 | + } |
| 48 | + } |
| 49 | +privateMap<Integer,Node> map; |
| 50 | +privatePriorityQueue<Node> pq; |
| 51 | +privateint capacity; |
| 52 | +privateint size; |
| 53 | +privateint tick; |
| 54 | +publicLFUCache(intcapacity) { |
| 55 | +this.map=newHashMap<>(); |
| 56 | +this.capacity= capacity; |
| 57 | + pq=newPriorityQueue<Node>((n1, n2)->{ |
| 58 | +return n1.freq== n2.freq? n1.tick- n2.tick: n1.freq- n2.freq; |
| 59 | + }); |
| 60 | + } |
| 61 | +publicintget(intkey) { |
| 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 | +publicvoidput(intkey,intvalue) { |
| 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=newNode(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 | +*Method2:Two hashMap+ doubleLinkedList O(1) |
| 104 | + ```Java |
| 105 | +classLFUCache { |
| 106 | +privatestaticfinalclassNode{ |
| 107 | +int key, val, freq; |
| 108 | +Node pre, next; |
| 109 | +publicNode(intkey,intval){ |
| 110 | +this.key= key; |
| 111 | +this.val= val; |
| 112 | +this.freq=1; |
| 113 | + } |
| 114 | + } |
| 115 | +privatestaticfinalclassDLLList{ |
| 116 | +Node head, tail; |
| 117 | +int size; |
| 118 | +DLLList(){ |
| 119 | + head=newNode(0,0); |
| 120 | + tail=newNode(0,0); |
| 121 | + head.next= tail; |
| 122 | + tail.pre= head; |
| 123 | + } |
| 124 | + |
| 125 | +voidadd(Nodenode){ |
| 126 | + head.next.pre= node; |
| 127 | + node.next= head.next; |
| 128 | + node.pre= head; |
| 129 | + head.next= node; |
| 130 | + size++; |
| 131 | + } |
| 132 | +voidremove(Nodenode){ |
| 133 | + node.pre.next= node.next; |
| 134 | + node.next.pre= node.pre; |
| 135 | + size--; |
| 136 | + } |
| 137 | +NoderemoveLast(){ |
| 138 | +if(size>0){ |
| 139 | +Node node= tail.pre; |
| 140 | + remove(node); |
| 141 | +return node; |
| 142 | + } |
| 143 | +returnnull; |
| 144 | + } |
| 145 | + } |
| 146 | +privateMap<Integer,Node> map; |
| 147 | +privateMap<Integer,DLLList> countMap; |
| 148 | +privateint size; |
| 149 | +privateint capacity; |
| 150 | +privateint min; |
| 151 | +publicLFUCache(intcapacity) { |
| 152 | +this.capacity= capacity; |
| 153 | +this.map=newHashMap<>(); |
| 154 | +this.countMap=newHashMap<>(); |
| 155 | + } |
| 156 | + |
| 157 | +publicintget(intkey) { |
| 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 | +publicvoidput(intkey,intvalue) { |
| 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=newNode(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,newDLLList()); |
| 182 | + newList.add(node); |
| 183 | + countMap.put(node.freq, newList); |
| 184 | + } |
| 185 | + } |
| 186 | +privatevoidupdate(Nodenode){ |
| 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,newDLLList()); |
| 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. [花花酱LeetCode460.LFUCache](http://zxi.mytechroad.com/blog/hashtable/leetcode-460-lfu-cache/) |