File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed
Linked_List/1756.Design-Most-Recently-Used-Queue Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change
1
+ class MRUQueue {
2
+ list<int>List;
3
+ vector<list<int>::iterator>skip;
4
+ int step;
5
+ public:
6
+ MRUQueue(int n)
7
+ {
8
+ step = sqrt(n+1);
9
+ for (int i=0; i<=n; i++)
10
+ {
11
+ List.push_back(i);
12
+ if (i%step==0)
13
+ skip.push_back(prev(List.end()));
14
+ }
15
+ }
16
+
17
+ int fetch(int k)
18
+ {
19
+ int t = k / step;
20
+ auto iter = skip[t];
21
+ for (int i=0; i< k%step; i++)
22
+ iter = next(iter);
23
+ int ret = *iter;
24
+
25
+ List.push_back(ret);
26
+
27
+ int j = (k%step==0)?t:t+1;
28
+ for (int i=j; i<skip.size(); i++)
29
+ skip[i] = next(skip[i]);
30
+
31
+ List.erase(iter);
32
+ return ret;
33
+ }
34
+ };
35
+
36
+ /**
37
+ * Your MRUQueue object will be instantiated and called as such:
38
+ * MRUQueue* obj = new MRUQueue(n);
39
+ * int param_1 = obj->fetch(k);
40
+ */
You can’t perform that action at this time.
0 commit comments