-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path0146-lru-cache.js
54 lines (50 loc) · 1.4 KB
/
0146-lru-cache.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* 146. LRU Cache
* https://leetcode.com/problems/lru-cache/
* Difficulty: Medium
*
* Design a data structure that follows the constraints of a Least Recently Used (LRU) cache.
*
* Implement the LRUCache class:
* - LRUCache(int capacity) Initialize the LRU cache with positive size capacity.
* - int get(int key) Return the value of the key if the key exists, otherwise return -1.
* - 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.
*
* The functions get and put must each run in O(1) average time complexity.
*/
/**
* @param {number} capacity
*/
var LRUCache = function(capacity) {
this.cache = new Map();
this.capacity = capacity;
};
/**
* @param {number} key
* @return {number}
*/
LRUCache.prototype.get = function(key) {
if (!this.cache.has(key)) {
return -1;
}
const value = this.cache.get(key);
this.cache.delete(key);
this.cache.set(key, value);
return this.cache.get(key);
};
/**
* @param {number} key
* @param {number} value
* @return {void}
*/
LRUCache.prototype.put = function(key, value) {
if (this.cache.has(key)) {
this.cache.delete(key);
}
this.cache.set(key, value);
if (this.cache.size > this.capacity) {
this.cache.delete(this.cache.keys().next().value);
}
};