-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue_Max_API.cpp
More file actions
76 lines (48 loc) · 926 Bytes
/
Copy pathQueue_Max_API.cpp
File metadata and controls
76 lines (48 loc) · 926 Bytes
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
#include <iostream>
#include <queue>
using namespace std;
/*
F B
Q:10 5 4 3 8
A:10 8
Enqueue: push()
1. > than back of A :erase from back until greater in A
2. < back of A enqueue(push back) in A
Dequeue:pop()
1. if == A front pop A front
2. If < A front - no op
max() : return A front
*/
class my_queue{
queue<int> Q;
deque<int> A;
public:
void push(int x){
Q.push(x);
if(A.empty() || x < A.back()) A.push_back(x);
else{
while(!A.empty() && A.back() < x) A.pop_back();
A.push_back(x);
}
}
void pop(){
int x = Q.front();
Q.pop();
if(x == A.front()) A.pop_front();
}
int max(){
return A.front();
}
};
int main() {
my_queue q;
q.push(10);
cout<<q.max()<<endl;
q.push(5);
cout<<q.max()<<endl;
q.push(4);
cout<<q.max()<<endl;
q.push(20);
cout<<q.max()<<endl;
return 0;
}