-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLRU_Cache.cpp
143 lines (120 loc) · 3.05 KB
/
LRU_Cache.cpp
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
/*
The task is to design and implement methods of an LRU cache. The class has two methods get() and set() which are defined as follows.
get(x) : Returns the value of the key x if the key exists in the cache otherwise returns -1.
set(x,y) : inserts the value if the key x is not already present. If the cache reaches its capacity it should invalidate the least recently used item before inserting the new item.
In the constructor of the class the size of the cache should be intitialized.
Example 1:
Input:
N = 2
Q = 2
Queries = SET 1 2 GET 1
Output: 2
Explanation: Cache Size = 2
SET 1 2 GET 1
SET 1 2 : 1 -> 2
GET 1 : Print the value corresponding
to Key 1, ie 2.
Example 2:
Input:
N = 2
Q = 7
Queries = SET 1 2 SET 2 3 SET 1 5
SET 4 5 SET 6 7 GET 4 GET 1
Output: 5 -1
Explanation: Cache Size = 2
SET 1 2 SET 2 3 SET 1 5 SET 4 5
SET 6 7 GET 4 GET 1
SET 1 2 : 1 -> 2
SET 2 3 : 1 -> 2, 2 -> 3 (the most
recently used one is kept at the
rightmost position)
SET 1 5 : 2 -> 3, 1 -> 5
SET 4 5 : 1 -> 5, 4 -> 5 (Cache size
is 2, hence we delete the least
recently used key-value pair)
SET 6 7 : 4 -> 5, 6 -> 7
GET 4 : Prints 5
GET 1 : No key-value pair having
key = 1. Hence prints -1.
Your Task:
You only need to complete the provided functions get() and set().
Expected Time Complexity: O(1) for both get() and set().
Expected Auxiliary Space: O(1) for both get() and set(). (though, you may use extra space for cache storage and implementation purposes).
Constraints:
1 <= N <= 1000
1 <= Q <= 100000
1 <= x, y <= 1000
*/
/
#include <bits/stdc++.h>
using namespace std;
class LRUCache
{
private:
list<int> cache;
unordered_map<int, pair<int, list<int>::iterator> > mp;
int csize;
public:
LRUCache(int capacity)
{
csize=capacity;
}
int get(int key)
{
if(mp.find(key)==mp.end()) return -1;
cache.erase(mp[key].second);
cache.push_front(key);
mp[key].second=cache.begin();
return mp[key].first;
}
void set(int key, int value)
{
if(mp.find(key)!=mp.end()){
// key found
cache.erase(mp[key].second);
} else {
// key not found
if(csize==cache.size()){
int last=cache.back();
cache.pop_back();
mp.erase(last);
}
}
cache.push_front(key);
mp[key]={value, cache.begin()};
}
};
int main()
{
int t;
cin >> t;
while (t--)
{
int capacity;
cin >> capacity;
LRUCache *cache = new LRUCache(capacity);
int queries;
cin >> queries;
while (queries--)
{
string q;
cin >> q;
if (q == "SET")
{
int key;
cin >> key;
int value;
cin >> value;
cache->set(key, value);
}
else
{
int key;
cin >> key;
cout << cache->get(key) << " ";
}
}
cout << endl;
}
return 0;
}