-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.cpp
More file actions
49 lines (42 loc) · 1.35 KB
/
Copy pathsolution.cpp
File metadata and controls
49 lines (42 loc) · 1.35 KB
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
/* C++ implementation */
#include <bits/stdc++.h>
using namespace std;
class SpecialQueue {
public:
queue<int> q; // main queue for FIFO
deque<int> minD; // monotonic increasing deque for minima
deque<int> maxD; // monotonic decreasing deque for maxima
SpecialQueue() {}
// Insert element x at rear
void enqueue(int x) {
q.push(x);
// maintain min deque (increasing)
while (!minD.empty() && minD.back() > x) minD.pop_back();
minD.push_back(x);
// maintain max deque (decreasing)
while (!maxD.empty() && maxD.back() < x) maxD.pop_back();
maxD.push_back(x);
}
// Remove element from front
void dequeue() {
if (q.empty()) return; // nothing to do if empty
int v = q.front(); q.pop();
if (!minD.empty() && minD.front() == v) minD.pop_front();
if (!maxD.empty() && maxD.front() == v) maxD.pop_front();
}
// Get front element
int getFront() {
if (q.empty()) return -1; // or throw
return q.front();
}
// Get minimum element
int getMin() {
if (minD.empty()) return -1;
return minD.front();
}
// Get maximum element
int getMax() {
if (maxD.empty()) return -1;
return maxD.front();
}
};