Skip to content

Commit b912c54

Browse files
Merge pull request #238 from Sakshamgupta90/master
Solved LRU_Cache.java
2 parents b99aa1c + 6e661e2 commit b912c54

1 file changed

Lines changed: 154 additions & 0 deletions

File tree

LeetCode/LRU_Cache.java

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
2+
// Leetcode Ques No. : 146
3+
// Problem Link: https://leetcode.com/problems/lru-cache/
4+
5+
6+
***** problem statement *****
7+
8+
/*
9+
Design a data structure that follows the constraints of a Least Recently Used (LRU) cache.
10+
Implement the LRUCache class:
11+
LRUCache(int capacity) Initialize the LRU cache with positive size capacity.
12+
int get(int key) Return the value of the key if the key exists, otherwise return -1.
13+
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.
14+
If the number of keys exceeds the capacity from this operation, evict the least recently used key.
15+
*/
16+
17+
18+
***** Solution *****
19+
20+
21+
22+
import java.util.HashMap;
23+
24+
public class LRUCache {
25+
// data Members
26+
private class ListNode {
27+
Integer key, value;
28+
ListNode next = null;
29+
ListNode prev = null;
30+
31+
ListNode(Integer key, Integer value) {
32+
this.key = key;
33+
this.value = value;
34+
}
35+
}
36+
37+
private HashMap<Integer, ListNode> map;
38+
private int capacity;
39+
private int size;
40+
private ListNode head = null;
41+
private ListNode tail = null;
42+
43+
private void intialize(int capacity) { //this function will initialise the capacity
44+
this.capacity = capacity;
45+
this.size = 0;
46+
this.map = new HashMap<>();
47+
this.head = this.tail = null; // this is the dummy node
48+
}
49+
50+
public LRUCache(int capacity) {
51+
intialize(capacity);
52+
}
53+
54+
private void addLast(ListNode node) { // in this function we will add every value of the node in the linkedlist
55+
if (this.head == null)
56+
this.head = this.tail = node;
57+
else {
58+
node.prev = this.tail;
59+
this.tail.next = node;
60+
this.tail = node;
61+
}
62+
this.size++;
63+
64+
}
65+
66+
private ListNode removeFirst() { // in this function the first node of the linkelist will be removed
67+
ListNode node = this.head;
68+
if (this.head == this.tail)
69+
this.head = this.tail = null;
70+
else {
71+
this.head = node.next;
72+
node.next = this.head.prev = null;
73+
}
74+
this.size--;
75+
return node;
76+
}
77+
78+
private ListNode removeLast() { //remove last node from LinkedList
79+
ListNode node = this.tail;
80+
if (this.head == this.tail)
81+
this.head = this.tail = null;
82+
else {
83+
this.tail = node.prev;
84+
node.prev = this.tail.next = null;
85+
}
86+
this.size--;
87+
return node;
88+
}
89+
90+
private ListNode remove(ListNode node) { //remove the node
91+
if (node == this.head)
92+
return removeFirst();
93+
else if (node == this.tail)
94+
return removeLast();
95+
else {
96+
ListNode prev = node.prev, forw = node.next;
97+
98+
prev.next = forw;
99+
forw.prev = prev;
100+
101+
node.next = node.prev = null;
102+
this.size--;
103+
return node;
104+
}
105+
}
106+
107+
private void makeRecent(ListNode node) { // in this we move the node to the front of linkedlist
108+
if (node == this.tail)
109+
return;
110+
111+
remove(node);
112+
addLast(node);
113+
}
114+
115+
public int get(int key) { // get the value and add in the linkedlist
116+
if (!map.containsKey(key)) //if value is not present in the linked list then return -1
117+
return -1;
118+
119+
ListNode node = map.get(key);
120+
makeRecent(node);
121+
return node.value;
122+
}
123+
124+
public void put(int key, int value) { //in this function whenever we put a value first we will check that whether that value is present or not in an hashmap
125+
if (map.containsKey(key)) {
126+
ListNode node = map.get(key); // if value is present then we will update the value of the node
127+
node.value = value;
128+
makeRecent(node);
129+
} else {
130+
ListNode node = new ListNode(key, value);
131+
if (this.size == this.capacity) {
132+
ListNode rn = removeFirst();
133+
map.remove(rn.key);
134+
}
135+
136+
addLast(node);
137+
map.put(key, node);
138+
}
139+
}
140+
}
141+
142+
143+
144+
145+
146+
/*
147+
Sample input: ["LRUCache","put","put","get","put","get","put","get","get","get"]
148+
[[2],[1,1],[2,2],[1],[3,3],[2],[4,4],[1],[3],[4]]
149+
150+
Sample Output: [null,null,null,1,null,-1,null,-1,3,4]
151+
152+
time complexity : O(n)
153+
space complexity : O(n)
154+
*/

0 commit comments

Comments
 (0)