|
| 1 | +/** |
| 2 | + * Design a data structure that follows the constraints of a Least Recently Used (LRU) cache. |
| 3 | +
|
| 4 | +Implement the LRUCache class: |
| 5 | +
|
| 6 | +LRUCache(int capacity) Initialize the LRU cache with positive size capacity. |
| 7 | +int get(int key) Return the value of the key if the key exists, otherwise return -1. |
| 8 | +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. |
| 9 | +Follow up: |
| 10 | +Could you do get and put in O(1) 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 | +Constraints: |
| 36 | +
|
| 37 | +1 <= capacity <= 3000 |
| 38 | +0 <= key <= 3000 |
| 39 | +0 <= value <= 104 |
| 40 | +At most 3 * 104 calls will be made to get and put. |
| 41 | + */ |
| 42 | + |
| 43 | +/** |
| 44 | + * @param {number} capacity |
| 45 | + */ |
| 46 | +var LRUCache = function (capacity) { |
| 47 | + this.cache = new Map(); |
| 48 | + this.capacity = capacity; |
| 49 | +}; |
| 50 | + |
| 51 | +/** |
| 52 | + * @param {number} key |
| 53 | + * @return {number} |
| 54 | + */ |
| 55 | +LRUCache.prototype.get = function (key) { |
| 56 | + if (!this.cache.has(key)) { |
| 57 | + return -1; |
| 58 | + } |
| 59 | + const value = this.cache.get(key); |
| 60 | + this.cache.delete(key); |
| 61 | + this.cache.set(key, value); |
| 62 | + return value; |
| 63 | +}; |
| 64 | + |
| 65 | +/** |
| 66 | + * @param {number} key |
| 67 | + * @param {number} value |
| 68 | + * @return {void} |
| 69 | + */ |
| 70 | +LRUCache.prototype.put = function (key, value) { |
| 71 | + if (this.capacity <= 0) { |
| 72 | + return; |
| 73 | + } |
| 74 | + if (this.cache.has(key)) { |
| 75 | + this.cache.delete(key); |
| 76 | + this.cache.set(key, value); |
| 77 | + return; |
| 78 | + } |
| 79 | + if (this.cache.size >= this.capacity) { |
| 80 | + const removedKey = this.cache.keys().next().value; |
| 81 | + this.cache.delete(removedKey); |
| 82 | + } |
| 83 | + this.cache.set(key, value); |
| 84 | +}; |
| 85 | + |
| 86 | +/** |
| 87 | + * Your LRUCache object will be instantiated and called as such: |
| 88 | + * var obj = new LRUCache(capacity) |
| 89 | + * var param_1 = obj.get(key) |
| 90 | + * obj.put(key,value) |
| 91 | + */ |
0 commit comments