Skip to content

Commit c415ae4

Browse files
committed
finish 146
1 parent ce672a2 commit c415ae4

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed

101-200/146. LRU Cache.md

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# 146. LRU Cache
2+
3+
- Difficulty: Hard.
4+
- Related Topics: Design.
5+
- Similar Questions: LFU Cache, Design In-Memory File System, Design Compressed String Iterator.
6+
7+
## Problem
8+
9+
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: ```get``` and ```put```.
10+
11+
```get(key)``` - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
12+
```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.
13+
14+
**Follow up:**
15+
Could you do both operations in **O(1)** time complexity?
16+
17+
**Example:**
18+
```
19+
LRUCache cache = new LRUCache( 2 /* capacity */ );
20+
21+
cache.put(1, 1);
22+
cache.put(2, 2);
23+
cache.get(1); // returns 1
24+
cache.put(3, 3); // evicts key 2
25+
cache.get(2); // returns -1 (not found)
26+
cache.put(4, 4); // evicts key 1
27+
cache.get(1); // returns -1 (not found)
28+
cache.get(3); // returns 3
29+
cache.get(4); // returns 4
30+
```
31+
32+
## Solution
33+
34+
```javascript
35+
var List = function (key, val) {
36+
this.key = key;
37+
this.val = val;
38+
this.next = null;
39+
this.prev = null;
40+
};
41+
42+
/**
43+
* @param {number} capacity
44+
*/
45+
var LRUCache = function(capacity) {
46+
this.capacity = capacity;
47+
this.length = 0;
48+
this.map = {};
49+
this.head = null;
50+
this.tail = null;
51+
};
52+
53+
/**
54+
* @param {number} key
55+
* @return {number}
56+
*/
57+
LRUCache.prototype.get = function(key) {
58+
var node = this.map[key];
59+
if (node) {
60+
this.remove(node);
61+
this.insert(node.key, node.val);
62+
return node.val;
63+
} else {
64+
return -1;
65+
}
66+
};
67+
68+
/**
69+
* @param {number} key
70+
* @param {number} value
71+
* @return {void}
72+
*/
73+
LRUCache.prototype.put = function(key, value) {
74+
if (this.map[key]) {
75+
this.remove(this.map[key]);
76+
this.insert(key, value);
77+
} else {
78+
if (this.length === this.capacity) {
79+
this.remove(this.head);
80+
this.insert(key, value);
81+
} else {
82+
this.insert(key, value);
83+
this.length++;
84+
}
85+
}
86+
};
87+
88+
/**
89+
* Your LRUCache object will be instantiated and called as such:
90+
* var obj = Object.create(LRUCache).createNew(capacity)
91+
* var param_1 = obj.get(key)
92+
* obj.put(key,value)
93+
*/
94+
95+
LRUCache.prototype.remove = function (node) {
96+
var prev = node.prev;
97+
var next = node.next;
98+
if (next) next.prev = prev;
99+
if (prev) prev.next = next;
100+
if (this.head === node) this.head = next;
101+
if (this.tail === node) this.tail = prev;
102+
delete this.map[node.key];
103+
};
104+
105+
LRUCache.prototype.insert = function (key, val) {
106+
var node = new List(key, val);
107+
if (!this.tail) {
108+
this.tail = node;
109+
this.head = node;
110+
} else {
111+
this.tail.next = node;
112+
node.prev = this.tail;
113+
this.tail = node;
114+
}
115+
this.map[key] = node;
116+
};
117+
```
118+
119+
**Explain:**
120+
121+
nope.
122+
123+
**Complexity:**
124+
125+
* Time complexity : O(1).
126+
* Space complexity : O(n).

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy