-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path59-2.cpp
47 lines (41 loc) · 832 Bytes
/
59-2.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
#include <bits/stdc++.h>
using namespace std;
class MaxQueue
{
private:
deque<int> q1, q2;
public:
MaxQueue()
{
}
int max_value()
{
return q2.empty() ? -1 : q2.front();
}
void push_back(int value)
{
while (!q2.empty() && q2.back() < value)
{
q2.pop_back();
};
q2.push_back(value);
q1.push_back(value);
}
int pop_front()
{
if (q1.empty())
return -1;
int ans = q1.front();
if (ans == q2.front())
q2.pop_front();
q1.pop_front();
return ans;
}
};
/**
* Your MaxQueue object will be instantiated and called as such:
* MaxQueue* obj = new MaxQueue();
* int param_1 = obj->max_value();
* obj->push_back(value);
* int param_3 = obj->pop_front();
*/