|
| 1 | +## 146. LRU Cache |
| 2 | + |
| 3 | +### Question |
| 4 | +Design and implement a data structure for Least Recently Used (LRU) 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 reached its capacity, it should invalidate the least recently used item before inserting a new item. |
| 8 | + |
| 9 | +Follow up: |
| 10 | +* Could you do both operations in O(1) time complexity? |
| 11 | + |
| 12 | +``` |
| 13 | +Example: |
| 14 | +
|
| 15 | +LRUCache cache = new LRUCache( 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.put(4, 4); // evicts key 1 |
| 23 | +cache.get(1); // returns -1 (not found) |
| 24 | +cache.get(3); // returns 3 |
| 25 | +cache.get(4); // returns 4 |
| 26 | +``` |
| 27 | + |
| 28 | +### Solution |
| 29 | +* Method 1: HashMap + Bidirectional list |
| 30 | + ```Java |
| 31 | + class LRUCache { |
| 32 | + private int capacity; |
| 33 | + private Map<Integer, DequeNode> map; |
| 34 | + private DequeNode dummy; |
| 35 | + private DequeNode tail; |
| 36 | + private int size; |
| 37 | + private static class DequeNode{ |
| 38 | + public int key; |
| 39 | + public int val; |
| 40 | + public DequeNode pre, next; |
| 41 | + public DequeNode(int key, int val){ |
| 42 | + this.key = key; |
| 43 | + this.val = val; |
| 44 | + } |
| 45 | + } |
| 46 | + public LRUCache(int capacity) { |
| 47 | + this.capacity = capacity; |
| 48 | + this.map = new HashMap<>(); |
| 49 | + this.size = 0; |
| 50 | + this.dummy = new DequeNode(-1, -1); |
| 51 | + } |
| 52 | + |
| 53 | + private void insertHead(DequeNode cur){ |
| 54 | + if(cur.pre != null){ |
| 55 | + cur.pre.next = cur.next; //remove current node from the list. |
| 56 | + if(cur.next != null) |
| 57 | + cur.next.pre = cur.pre; |
| 58 | + } |
| 59 | + DequeNode originalHead = dummy.next; //insert current node to the head of the list. |
| 60 | + cur.pre = dummy; |
| 61 | + dummy.next = cur; |
| 62 | + cur.next = originalHead; |
| 63 | + if(originalHead != null){ |
| 64 | + originalHead.pre = cur; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + public int get(int key) { |
| 69 | + if(!map.containsKey(key)) return -1; |
| 70 | + DequeNode cur = map.get(key); |
| 71 | + if(cur == tail && size != 1){ |
| 72 | + this.tail = this.tail.pre; |
| 73 | + this.tail.next = null; |
| 74 | + } |
| 75 | + insertHead(cur); |
| 76 | + return cur.val; |
| 77 | + } |
| 78 | + |
| 79 | + public void put(int key, int value) { |
| 80 | + if(map.containsKey(key)){ |
| 81 | + DequeNode cur = map.get(key); |
| 82 | + if(cur == tail && size != 1){ |
| 83 | + tail = cur.pre; |
| 84 | + } |
| 85 | + cur.val = value; |
| 86 | + insertHead(cur); |
| 87 | + }else{ |
| 88 | + DequeNode node = new DequeNode(key, value); |
| 89 | + this.map.put(key, node); |
| 90 | + if(size + 1 <= this.capacity){ |
| 91 | + if(size + 1 == 1){ |
| 92 | + this.tail = node; |
| 93 | + } |
| 94 | + insertHead(node); |
| 95 | + size++; |
| 96 | + }else{ |
| 97 | + // Need to remove the last node and insert current one. |
| 98 | + insertHead(node); |
| 99 | + DequeNode originalTail = this.tail; |
| 100 | + map.remove(originalTail.key); |
| 101 | + this.tail = this.tail.pre; |
| 102 | + this.tail.next = null; |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Your LRUCache object will be instantiated and called as such: |
| 110 | + * LRUCache obj = new LRUCache(capacity); |
| 111 | + * int param_1 = obj.get(key); |
| 112 | + * obj.put(key,value); |
| 113 | + */ |
| 114 | + ``` |
0 commit comments