Skip to content

Commit 1ff5954

Browse files
committed
Adding readme file
1 parent 138405c commit 1ff5954

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

146-LRUCache/README.md

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
Design a data structure that follows the constraints of a Least Recently Used (LRU) cache.
2+
3+
Implement the LRUCache class:
4+
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+
10+
11+
The functions get and put must each run in O(1) average time complexity.
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+
 
36+
Constraints:
37+
38+
39+
1 <= capacity <= 3000
40+
0 <= key <= 104
41+
0 <= value <= 105
42+
At most 2 * 105 calls will be made to get and put.
43+

0 commit comments

Comments
 (0)