-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlru_cache.dart
285 lines (230 loc) · 6.03 KB
/
lru_cache.dart
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
-* 146. LRU Cache *-
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.
The functions get and put must each run in O(1) average time complexity.
Example 1:
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]]
Output
[null, null, null, 1, null, -1, null, -1, 3, 4]
Explanation
LRUCache lRUCache = new LRUCache(2);
lRUCache.put(1, 1); // cache is {1=1}
lRUCache.put(2, 2); // cache is {1=1, 2=2}
lRUCache.get(1); // return 1
lRUCache.put(3, 3); // LRU key was 2, evicts key 2, cache is {1=1, 3=3}
lRUCache.get(2); // returns -1 (not found)
lRUCache.put(4, 4); // LRU key was 1, evicts key 1, cache is {4=4, 3=3}
lRUCache.get(1); // return -1 (not found)
lRUCache.get(3); // return 3
lRUCache.get(4); // return 4
Constraints:
1 <= capacity <= 3000
0 <= key <= 104
0 <= value <= 105
At most 2 * 105 calls will be made to get and put.
class LRUCache {
LRUCache(int capacity) {
}
int get(int key) {
}
void put(int key, int value) {
}
}
/**
* Your LRUCache object will be instantiated and called as such:
* LRUCache obj = LRUCache(capacity);
* int param1 = obj.get(key);
* obj.put(key,value);
*/
*/
// class LRUCache {
// LRUCache(int capacity) {
// }
// int get(int key) {
// }
// void put(int key, int value) {
// }
// }
// import 'dart:collection';
// class LRUCache {
// late int cnt;
// Map<int, int> mp = {};
// Queue<int> dq = Queue();
// Set<MapEntry<int, int>> st = {};
// Map<int, int> val = {};
// int res = 0;
// LRUCache(int capacity) {
// cnt = capacity;
// }
// int get(int key) {
// if (mp.containsKey(key)) {
// int v = val[key]!;
// st.remove(MapEntry(v, key));
// res++;
// st.add(MapEntry(res, key));
// val[key] = res;
// return mp[key]!;
// }
// return -1; // Return -1 if the key is not present in the cache
// }
// void put(int key, int value) {
// if (!mp.containsKey(key)) {
// if (mp.length == cnt) {
// int r = dq.removeFirst();
// st.remove(MapEntry(val[r]!, r));
// mp.remove(r);
// val.remove(r);
// }
// res++;
// mp[key] = value;
// val[key] = res;
// st.add(MapEntry(res, key));
// dq.addLast(key);
// } else {
// int v = val[key]!;
// st.remove(MapEntry(v, key));
// res++;
// st.add(MapEntry(res, key));
// mp[key] = value;
// val[key] = res;
// }
// }
// }
import 'dart:collection';
class LRUCacheOne {
final Queue<int> leastUsedCache = Queue<int>();
final List<int> usages = List<int>.filled(10001, 0);
final List<int> keyValuePair = List<int>.filled(10001, -1);
int size = 0;
int cap = 0;
LRUCacheOne(int capacity) {
cap = capacity;
}
int get(int key) {
if (keyValuePair[key] != -1) {
leastUsedCache.add(key);
usages[key]++;
}
return keyValuePair[key];
}
void put(int key, int value) {
if (size < cap || keyValuePair[key] != -1) {
if (keyValuePair[key] == -1) size++;
leastUsedCache.add(key);
usages[key]++;
keyValuePair[key] = value;
return;
}
while (usages[leastUsedCache.first] != 1) {
usages[leastUsedCache.first]--;
leastUsedCache.removeFirst();
}
keyValuePair[leastUsedCache.first] = -1;
usages[leastUsedCache.first]--;
leastUsedCache.removeFirst();
leastUsedCache.add(key);
usages[key]++;
keyValuePair[key] = value;
}
}
class LRUCacheFAST {
late int k;
final Map<int, List<int>> mp = Map();
final Queue<int> q = Queue<int>();
LRUCacheFAST(int capacity) {
k = capacity;
}
int get(int key) {
if (mp.containsKey(key)) {
mp[key]![1]++;
q.add(key);
return mp[key]![0];
}
return -1;
}
void put(int key, int value) {
if (mp.containsKey(key)) {
mp[key]![0] = value;
mp[key]![1]++;
q.add(key);
} else {
mp[key] = [value, 1];
q.add(key);
}
// Check if capacity is exceeded.
if (mp.length > k) {
while (q.isNotEmpty) {
int ky = q.removeFirst();
// Decrement the frequency of ky.
mp[ky]![1]--;
// If no more key present in the queue, that means it is the least recently used.
if (mp[ky]![1] == 0) {
mp.remove(ky);
break;
}
}
}
}
}
//
class Node {
late final int key;
late final int val;
Node? prev = null;
Node? next = null;
Node(this.key, this.val);
}
class LRUCache {
late int cap;
final Map<int, Node> mp = {};
final Node head = Node(-1, -1);
final Node tail = Node(-1, -1);
LRUCache(int capacity) {
cap = capacity;
head.next = tail;
tail.prev = head;
}
void addNode(Node newNode) {
Node? temp = head.next;
newNode.next = temp;
newNode.prev = head;
head.next = newNode;
temp?.prev = newNode;
}
void deleteNode(Node delNode) {
Node? delPrev = delNode.prev;
Node? delNext = delNode.next;
delPrev?.next = delNext;
delNext?.prev = delPrev;
}
int get(int key) {
if (mp.containsKey(key)) {
Node resNode = mp[key]!;
int res = resNode.val;
deleteNode(resNode);
addNode(resNode);
mp[key] = head.next!;
return res;
}
return -1;
}
void put(int key, int value) {
if (mp.containsKey(key)) {
Node existingNode = mp[key]!;
mp.remove(key);
deleteNode(existingNode);
}
if (mp.length == cap) {
mp.remove(tail.prev!.key);
deleteNode(tail.prev!);
}
addNode(Node(key, value));
mp[key] = head.next!;
}
}