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
* Logic : maintain doubly linked List for insertion at head and deletion from tail and maintain hashmap of key as key and value as node. Node consists of key, value, prev and next pointers.
5
+
* Runtime: 16 ms
6
+
* Memory Usage: 47.9 MB
7
+
*/
8
+
9
+
classLRUCache {
10
+
11
+
finalNodehead = newNode();
12
+
finalNodetail = newNode();
13
+
intcapacity;
14
+
Map<Integer, Node> map;
15
+
16
+
publicLRUCache(intcapacity) {
17
+
map = newHashMap(capacity);
18
+
this.capacity = capacity;
19
+
head.next = tail;
20
+
tail.prev = head;
21
+
}
22
+
23
+
publicintget(intkey) {
24
+
intresult = -1;
25
+
Nodenode = map.get(key);
26
+
if(node!=null)
27
+
{
28
+
remove(node);
29
+
add(node);
30
+
result = node.val;
31
+
}
32
+
returnresult;
33
+
}
34
+
35
+
publicvoidput(intkey, intvalue) {
36
+
Nodenode = map.get(key);
37
+
if(node!=null)
38
+
{
39
+
remove(node);
40
+
node.val = value;
41
+
add(node);
42
+
}
43
+
else{
44
+
if(map.size() == capacity)
45
+
{
46
+
map.remove(tail.prev.key);
47
+
remove(tail.prev);
48
+
}
49
+
Nodenew_node = newNode();
50
+
51
+
new_node.key = key;
52
+
new_node.val = value;
53
+
map.put(key, new_node);
54
+
add(new_node);
55
+
}
56
+
}
57
+
58
+
publicvoidadd(Nodenode)
59
+
{
60
+
Nodehead_next = head.next;
61
+
node.next = head_next;
62
+
head_next.prev = node;
63
+
head.next = node;
64
+
node.prev = head;
65
+
66
+
}
67
+
68
+
publicvoidremove(Nodenode)
69
+
{
70
+
Nodenext_node = node.next;
71
+
Nodeprev_node = node.prev;
72
+
73
+
next_node.prev = prev_node;
74
+
prev_node.next = next_node;
75
+
76
+
}
77
+
78
+
classNode{
79
+
intkey;
80
+
intval;
81
+
Nodeprev;
82
+
Nodenext;
83
+
}
84
+
}
85
+
86
+
/**
87
+
* Your LRUCache object will be instantiated and called as such:
|[Yuri Spiridonov](https://github.com/YuriSpiridonov) <br> <imgsrc="https://github.com/YuriSpiridonov.png"width="100"height="100"> | Russia | Python |[Twitter](https://twitter.com/YuriSpiridonov)<br>[Leetcode](https://leetcode.com/yurispiridonov/)<br>[Hackerrank](https://www.hackerrank.com/YuriSpiridonov)|
188
220
|[Naveen Kashyap](https://github.com/naveenkash) <br> <imgsrc="https://github.com/naveenkash.png"width="100"height="100"> | India | Javascript |[Twitter](https://twitter.com/naveen_kashyapp)<br>[Leetcode](https://leetcode.com/naveenkash/)|
221
+
|[Rudra Mishra](https://github.com/Rudra407) <br> <imgsrc="https://github.com/Rudra407.png"width="100"height="100"> | India | C++ |[Twitter](https://twitter.com/ruDra_Mishra407)<br>[Leetcode](https://leetcode.com/rudramishra/)|
189
222
|[Sachin Singh Negi](https://github.com/sachinnegi) <br> <imgsrc="https://github.com/sachinnegi.png"width="100"height="100"> | India | Python |[Twitter](https://twitter.com/SachinSinghNe17)<br>[Leetcode](https://leetcode.com/negisachin688/)<br>[Hackerrrak](https://www.hackerrank.com/negisachin688)||
0 commit comments