forked from prateekshyap/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LRUCache.java
187 lines (169 loc) · 4.43 KB
/
LRUCache.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*https://leetcode.com/problems/lru-cache/*/
//LinkedHashMap implementation
class LRUCache {
static LinkedHashMap<Integer,Integer> map;
static int capacity;
static Iterator iterator;
public LRUCache(int capacity) {
this.map = new LinkedHashMap<Integer,Integer>(capacity);
this.capacity = capacity;
}
public int get(int key) {
//get the value
int result = map.containsKey(key) ? map.get(key) : -1;
//if key is present
if (result != -1)
{
//remove and reinsert it
map.remove(key);
map.put(key,result);
}
return result;
}
public void put(int key, int value) {
//if the key is present
if (map.containsKey(key))
{
//remove and reinsert it
map.remove(key);
map.put(key,value);
}
//if key is not present and no overflow
else if (map.size() < capacity)
{
//add the key
map.put(key,value);
}
//all other cases
else
{
//remove the element at 0th index
iterator = map.entrySet().iterator();
if (iterator.hasNext())
{
int remove = (Integer)((Map.Entry)iterator.next()).getKey();
map.remove(remove);
}
//add the new key
map.put(key,value);
}
}
}
//scratch implementation
class Node
{
Node prev;
int key;
int value;
Node next;
Node(int k, int v)
{
key = k;
value = v;
}
}
class Cache
{
private Node head, tail;
private Node[] hashTable;
private int capacity;
private int size;
Cache(int capacity)
{
this.capacity = capacity;
size = 0;
//two dummy nodes to handle edge cases
head = new Node(-1,-1);
tail = new Node(-1,-1);
head.next = tail;
tail.prev = head;
hashTable = new Node[10001];
}
private void bubbleUp(int key)
{
hashTable[key].prev.next = hashTable[key].next;
hashTable[key].next.prev = hashTable[key].prev;
hashTable[key].prev = tail.prev;
hashTable[key].next = tail;
hashTable[key].prev.next = hashTable[key];
tail.prev = hashTable[key];
}
public void add(int key, int value)
{
hashTable[key] = new Node(key,value);
hashTable[key].prev = tail.prev;
hashTable[key].next = tail;
hashTable[key].prev.next = hashTable[key];
tail.prev = hashTable[key];
++size;
}
public void updateAndBubbleUp(int key, int value)
{
hashTable[key].value = value;
bubbleUp(key);
}
public void removeLRU()
{
//the next node to head dummy is the least recently used node
int key = head.next.key;
hashTable[key].next.prev = head;
head.next = hashTable[key].next;
hashTable[key].prev = null;
hashTable[key].next = null;
hashTable[key] = null;
}
public int getValue(int key)
{
bubbleUp(key);
return hashTable[key].value;
}
public boolean isPresent(int key)
{
return hashTable[key] == null ? false : true;
}
public boolean isOverflow()
{
return size > capacity ? true : false;
}
}
class LRUCache {
Cache cache;
public LRUCache(int capacity) {
cache = new Cache(capacity);
}
public int get(int key) {
if (cache.isPresent(key))
return cache.getValue(key);
return -1;
}
public void put(int key, int value) {
if (cache.isPresent(key))
cache.updateAndBubbleUp(key,value);
else
{
cache.add(key,value);
if (cache.isOverflow())
cache.removeLRU();
}
}
}
/*https://practice.geeksforgeeks.org/problems/page-faults-in-lru5603/1*/
/*almost same kind of cache and node implementation
along with this solution class*/
class Solution{
static int pageFaults(int N, int C, int pages[]){
Cache cache = new Cache(C);
int faults = 0;
for (int key : pages)
{
if (cache.isPresent(key)) cache.bubbleUp(key);
else
{
cache.add(key);
if (cache.isOverflow()) cache.removeLRU();
++faults;
}
}
return faults;
}
}