|
| 1 | +[](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby) |
| 2 | +[](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby/fork) |
| 3 | + |
| 4 | +## 146\. LRU Cache |
| 5 | + |
| 6 | +Medium |
| 7 | + |
| 8 | +Design a data structure that follows the constraints of a **[Least Recently Used (LRU) cache](https://en.wikipedia.org/wiki/Cache_replacement_policies#LRU)**. |
| 9 | + |
| 10 | +Implement the `LRUCache` class: |
| 11 | + |
| 12 | +* `LRUCache(int capacity)` Initialize the LRU cache with **positive** size `capacity`. |
| 13 | +* `int get(int key)` Return the value of the `key` if the key exists, otherwise return `-1`. |
| 14 | +* `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. |
| 15 | + |
| 16 | +The functions `get` and `put` must each run in `O(1)` average time complexity. |
| 17 | + |
| 18 | +**Example 1:** |
| 19 | + |
| 20 | +**Input** ["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"] [[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]] |
| 21 | + |
| 22 | +**Output:** [null, null, null, 1, null, -1, null, -1, 3, 4] |
| 23 | + |
| 24 | +**Explanation:** |
| 25 | + |
| 26 | + LRUCache lRUCache = new LRUCache(2); |
| 27 | + lRUCache.put(1, 1); // cache is {1=1} |
| 28 | + lRUCache.put(2, 2); // cache is {1=1, 2=2} |
| 29 | + lRUCache.get(1); // return 1 |
| 30 | + lRUCache.put(3, 3); // LRU key was 2, evicts key 2, cache is {1=1, 3=3} |
| 31 | + lRUCache.get(2); // returns -1 (not found) |
| 32 | + lRUCache.put(4, 4); // LRU key was 1, evicts key 1, cache is {4=4, 3=3} |
| 33 | + lRUCache.get(1); // return -1 (not found) |
| 34 | + lRUCache.get(3); // return 3 |
| 35 | + lRUCache.get(4); // return 4 |
| 36 | + |
| 37 | +**Constraints:** |
| 38 | + |
| 39 | +* `1 <= capacity <= 3000` |
| 40 | +* <code>0 <= key <= 10<sup>4</sup></code> |
| 41 | +* <code>0 <= value <= 10<sup>5</sup></code> |
| 42 | +* At most 2<code> * 10<sup>5</sup></code> calls will be made to `get` and `put`. |
| 43 | + |
| 44 | +## Solution |
| 45 | + |
| 46 | +```ruby |
| 47 | +class LRUCache |
| 48 | + class LruCacheNode |
| 49 | + attr_accessor :key, :value, :prev, :next |
| 50 | + |
| 51 | + def initialize(k, v) |
| 52 | + @key = k |
| 53 | + @value = v |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | +=begin |
| 58 | + :type capacity: Integer |
| 59 | +=end |
| 60 | + def initialize(cap) |
| 61 | + @capacity = cap |
| 62 | + @cache_map = {} |
| 63 | + # insert here |
| 64 | + @head = nil |
| 65 | + # remove here |
| 66 | + @tail = nil |
| 67 | + end |
| 68 | + |
| 69 | +=begin |
| 70 | + :type key: Integer |
| 71 | + :rtype: Integer |
| 72 | +=end |
| 73 | + def get(key) |
| 74 | + val_node = @cache_map[key] |
| 75 | + return -1 if val_node.nil? |
| 76 | + |
| 77 | + move_to_head(val_node) |
| 78 | + val_node.value |
| 79 | + end |
| 80 | + |
| 81 | +=begin |
| 82 | + :type key: Integer |
| 83 | + :type value: Integer |
| 84 | + :rtype: Void |
| 85 | +=end |
| 86 | + def put(key, value) |
| 87 | + val_node = @cache_map[key] |
| 88 | + |
| 89 | + if val_node |
| 90 | + val_node.value = value |
| 91 | + move_to_head(val_node) |
| 92 | + else |
| 93 | + if @cache_map.size < @capacity |
| 94 | + if @cache_map.empty? |
| 95 | + node = LruCacheNode.new(key, value) |
| 96 | + @cache_map[key] = node |
| 97 | + @head = node |
| 98 | + @tail = node |
| 99 | + else |
| 100 | + node = LruCacheNode.new(key, value) |
| 101 | + @cache_map[key] = node |
| 102 | + node.next = @head |
| 103 | + @head.prev = node |
| 104 | + @head = node |
| 105 | + end |
| 106 | + else |
| 107 | + # remove from tail |
| 108 | + last = @tail |
| 109 | + @tail = last.prev |
| 110 | + @tail.next = nil unless @tail.nil? |
| 111 | + @cache_map.delete(last.key) |
| 112 | + @head = nil if @cache_map.empty? |
| 113 | + # Call recursively |
| 114 | + put(key, value) |
| 115 | + end |
| 116 | + end |
| 117 | + end |
| 118 | + |
| 119 | + private |
| 120 | + |
| 121 | + def move_to_head(node) |
| 122 | + return if node == @head |
| 123 | + |
| 124 | + @tail = node.prev if node == @tail |
| 125 | + prev = node.prev |
| 126 | + nex = node.next |
| 127 | + prev.next = nex |
| 128 | + nex.prev = prev unless nex.nil? |
| 129 | + node.prev = nil |
| 130 | + node.next = @head |
| 131 | + @head.prev = node |
| 132 | + @head = node |
| 133 | + end |
| 134 | +end |
| 135 | + |
| 136 | +# Your LRUCache object will be instantiated and called as such: |
| 137 | +# obj = LRUCache.new(capacity) |
| 138 | +# param_1 = obj.get(key) |
| 139 | +# obj.put(key, value) |
| 140 | +``` |
0 commit comments