You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/data-structures/custom/lru-cache.js
+12-8
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,13 @@
1
1
2
2
/**
3
-
* Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put.
3
+
* Design and implement a data structure for Least Recently Used (LRU) cache.
4
+
* It should support the following operations: get and put.
4
5
5
-
get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
6
-
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.
6
+
get(key) - Get the value (will always be positive) of the key
7
+
if the key exists in the cache, otherwise return -1.
8
+
put(key, value) - Set or insert the value if the key is not already present.
9
+
When the cache reached its capacity, it should invalidate the least
10
+
recently used item before inserting a new item.
7
11
8
12
Follow up:
9
13
Could you do both operations in O(1) time complexity?
@@ -27,7 +31,7 @@
27
31
*
28
32
* @param {number} capacity
29
33
*/
30
-
constLRUCache=function(capacity){
34
+
constLRUCache=(capacity)=>{
31
35
this.map=newMap();
32
36
this.capacity=capacity;
33
37
};
@@ -36,7 +40,7 @@ const LRUCache = function (capacity) {
36
40
* @param {number} key
37
41
* @return {number}
38
42
*/
39
-
LRUCache.prototype.get=function(key){
43
+
LRUCache.prototype.get=(key)=>{
40
44
constvalue=this.map.get(key);
41
45
if(value){
42
46
this.moveToTop(key);
@@ -50,20 +54,20 @@ LRUCache.prototype.get = function (key) {
0 commit comments