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