Skip to content

Commit 8747cb9

Browse files
ongzxChi Hi OngGouravRusiya30
authored
Add solution for leetcode #146 (#67)
Co-authored-by: Chi Hi Ong <[email protected]> Co-authored-by: Gourav Rusiya <[email protected]>
1 parent 50e7143 commit 8747cb9

File tree

2 files changed

+93
-1
lines changed

2 files changed

+93
-1
lines changed

JavaScript/146.LRU-Cache.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
*/

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,8 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
205205
| 242 | [Valid Anagram](https://leetcode.com/problems/valid-anagram/) | [Java](./Java/valid-anagram.java) | _O(n)_ | _O(1)_ | Easy | | [Tutorial](https://www.youtube.com/watch?v=sbX1Ze9lNQE) |
206206
| 146 | [LRU Cache](https://leetcode.com/problems/lru-cache/) | [Java](./Java/LRU-Cache.java) | | | Medium | | |
207207
| 217 | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate/) | [Python](./Python/contains-duplicate.py) | _O(n)_ | _O(n)_ | | |
208-
| 554 | [Brick Wall](https://leetcode.com/problems/brick-wall/) | [C++](./C++/brick-walls.cpp)| _O(n)_ | _O(n)_ | Medium | |
208+
| 554 | [Brick Wall](https://leetcode.com/problems/brick-wall/) | [C++](./C++/brick-walls.cpp) | _O(n)_ | _O(n)_ | Medium | |
209+
| 146 | [LRU Cache](https://leetcode.com/problems/lru-cache/) | [Javascript](../JavaScript/146.LRU-Cache.js) | _O(log(n))_ | _O(n)_ | Medium | |
209210

210211
<br/>
211212
<div align="right">

0 commit comments

Comments
 (0)