|
| 1 | +// 146. LRU Cache |
| 2 | + |
| 3 | +// Design a data structure that follows the constraints of a Least Recently Used (LRU) cache. |
| 4 | + |
| 5 | +// Implement the LRUCache class: |
| 6 | + |
| 7 | +// LRUCache(int capacity) Initialize the LRU cache with positive size capacity. |
| 8 | +// int get(int key) Return the value of the key if the key exists, otherwise return -1. |
| 9 | +// void put(int key, int value) Update the value of the key if the key exists. Otherwise, add the key-value pair to the cache. If the number of keys exceeds the capacity from this operation, evict the least recently used key. |
| 10 | +// The functions get and put must each run in O(1) average time complexity. |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +// Example 1: |
| 15 | + |
| 16 | +// Input |
| 17 | +// ["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"] |
| 18 | +// [[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]] |
| 19 | +// Output |
| 20 | +// [null, null, null, 1, null, -1, null, -1, 3, 4] |
| 21 | + |
| 22 | +// Explanation |
| 23 | +// LRUCache lRUCache = new LRUCache(2); |
| 24 | +// lRUCache.put(1, 1); // cache is {1=1} |
| 25 | +// lRUCache.put(2, 2); // cache is {1=1, 2=2} |
| 26 | +// lRUCache.get(1); // return 1 |
| 27 | +// lRUCache.put(3, 3); // LRU key was 2, evicts key 2, cache is {1=1, 3=3} |
| 28 | +// lRUCache.get(2); // returns -1 (not found) |
| 29 | +// lRUCache.put(4, 4); // LRU key was 1, evicts key 1, cache is {4=4, 3=3} |
| 30 | +// lRUCache.get(1); // return -1 (not found) |
| 31 | +// lRUCache.get(3); // return 3 |
| 32 | +// lRUCache.get(4); // return 4 |
| 33 | + |
| 34 | + |
| 35 | +// |
| 36 | +public class LRUCache { |
| 37 | +private readonly int _capacity; |
| 38 | + private readonly Dictionary<int, LinkedListNode<KeyValuePair<int, int>>> _cache; |
| 39 | + private readonly LinkedList<KeyValuePair<int, int>> _lru; |
| 40 | + |
| 41 | + public LRUCache(int capacity) |
| 42 | + { |
| 43 | + _capacity = capacity; |
| 44 | + _cache = new(); |
| 45 | + _lru = new(); |
| 46 | + } |
| 47 | + |
| 48 | + public int Get(int key) |
| 49 | + { |
| 50 | + if (!_cache.TryGetValue(key, out var node)) |
| 51 | + { |
| 52 | + return -1; |
| 53 | + } |
| 54 | + |
| 55 | + _lru.Remove(node); |
| 56 | + _cache[key] = _lru.AddFirst(node.Value); |
| 57 | + |
| 58 | + return node.Value.Value; |
| 59 | + } |
| 60 | + |
| 61 | + public void Put(int key, int value) |
| 62 | + { |
| 63 | + if (_cache.TryGetValue(key, out var node)) |
| 64 | + { |
| 65 | + _lru.Remove(node); |
| 66 | + _cache.Remove(key); |
| 67 | + } |
| 68 | + |
| 69 | + node = _lru.AddFirst(new KeyValuePair<int, int>(key, value)); |
| 70 | + _cache[key] = node; |
| 71 | + |
| 72 | + if (_lru.Count > _capacity) |
| 73 | + { |
| 74 | + node = _lru.Last; |
| 75 | + _lru.RemoveLast(); |
| 76 | + _cache.Remove(node.Value.Key); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +//self defined DLL |
| 82 | +public class LRUCache { |
| 83 | + public class DoublyLinkList |
| 84 | + { |
| 85 | + public (int key, int value) val; |
| 86 | + public DoublyLinkList prev; |
| 87 | + public DoublyLinkList next; |
| 88 | + public DoublyLinkList((int, int) val, DoublyLinkList prev = null, |
| 89 | + DoublyLinkList next = null) |
| 90 | + { |
| 91 | + this.val = val; |
| 92 | + this.prev = prev; |
| 93 | + this.next = next; |
| 94 | + } |
| 95 | + } |
| 96 | + DoublyLinkList root; |
| 97 | + DoublyLinkList tail; |
| 98 | + Dictionary<int, DoublyLinkList> cache; |
| 99 | + int capacity; |
| 100 | + public LRUCache(int capacity) |
| 101 | + { |
| 102 | + cache = new Dictionary<int, DoublyLinkList>(); |
| 103 | + this.capacity = capacity; |
| 104 | + root = tail = null; |
| 105 | + } |
| 106 | + |
| 107 | + public int Get(int key) |
| 108 | + { |
| 109 | + if (cache.ContainsKey(key)) |
| 110 | + { |
| 111 | + Remove(cache[key]); |
| 112 | + Add(cache[key]); |
| 113 | + return cache[key].val.value; |
| 114 | + } |
| 115 | + return -1; |
| 116 | + } |
| 117 | + |
| 118 | + public void Put(int key, int value) |
| 119 | + { |
| 120 | + if (cache.ContainsKey(key)) |
| 121 | + { |
| 122 | + Remove(cache[key]); |
| 123 | + cache.Remove(key); |
| 124 | + } |
| 125 | + DoublyLinkList node = new DoublyLinkList((key, value)); |
| 126 | + Add(node); |
| 127 | + cache.Add(key, node); |
| 128 | + if(cache.Count == capacity) |
| 129 | + { |
| 130 | + Remove(root); |
| 131 | + cache.Remove(root.val.key); |
| 132 | + } |
| 133 | + } |
| 134 | + public void Add(DoublyLinkList node) |
| 135 | + { |
| 136 | + if (root == null) |
| 137 | + { |
| 138 | + root = node; |
| 139 | + tail = node; |
| 140 | + } |
| 141 | + tail.next = node; |
| 142 | + node.prev = tail; |
| 143 | + tail = node; |
| 144 | + } |
| 145 | + public void Remove(DoublyLinkList node) |
| 146 | + { |
| 147 | + if (root == tail) |
| 148 | + root = tail = null; |
| 149 | + else if(node == root) |
| 150 | + { |
| 151 | + root = root.next; |
| 152 | + root.prev = null; |
| 153 | + } |
| 154 | + else if(node == tail) |
| 155 | + { |
| 156 | + tail = tail.prev; |
| 157 | + tail.next = null; |
| 158 | + } |
| 159 | + else |
| 160 | + { |
| 161 | + DoublyLinkList Prev = node.prev; |
| 162 | + DoublyLinkList Next = node.next; |
| 163 | + if (Prev != null) |
| 164 | + Prev.next = Next; |
| 165 | + if (Next != null) |
| 166 | + Next.prev = Prev; |
| 167 | + } |
| 168 | + } |
| 169 | +} |
0 commit comments