-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmax_queue.cpp
212 lines (190 loc) · 5.24 KB
/
max_queue.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* MAXIMUM ELEMENT IN A QUEUE (MAX QUEUE)
*
* This task involves implementing a data structure called MaxQueue that supports the
* following operations:
* - push(x): Insert an integer x into the queue.
* - pop(): Remove the front element from the queue.
* - max(): Retrieve the maximum element currently in the queue.
*
* Two main approaches are provided:
*
* 1. Simple (Brute-force) Solution:
* Uses a standard queue (implemented with std::deque) and, for each max() call,
* scans through all elements to find the maximum. This approach is straightforward
* but inefficient with a complexity of O(n) per max() operation.
*
* 2. Optimal (Efficient) Solution:
* Uses a standard queue along with an auxiliary deque that maintains potential
* maximum candidates. This structure enables all operations to run in O(1) amortized
* time.
*
* 3. Alternative (Educational) Solution:
* Uses a multiset to maintain the queue elements in sorted order. With each push and
* pop, the multiset is updated accordingly. This approach achieves O(log n) operations,
* demonstrating an alternative trade-off between simplicity and performance.
*
* ASCII Illustration:
*
* Operations:
* push(3) -> Queue: [3] | max: 3
* push(1) -> Queue: [3,1] | max: 3
* push(5) -> Queue: [3,1,5] | max: 5
* pop() -> Queue: [1,5] | max: 5
*
* Example:
* Given the following sequence of operations:
* push 3, push 1, push 5, max, pop, max
* The expected output from the max() operations is:
* 5, 5
*
* Explanation:
* - After inserting 3, 1, and 5, the maximum element is 5.
* - After a pop operation (removing 3), the maximum remains 5.
*/
#include <cassert>
#include <deque>
#include <iostream>
#include <limits>
#include <optional>
#include <queue>
#include <set>
#include <vector>
// Simple (Brute-force) Solution
// Uses a deque for queue operations and scans the entire queue on each max() call.
// Complexity: O(n) for max() operation.
class SimpleMaxQueue {
public:
void push(int x) {
q.push_back(x);
}
void pop() {
if (!q.empty()) {
q.pop_front();
}
}
int max() const {
assert(!q.empty());
int m = std::numeric_limits<int>::min();
for (int x : q) {
m = std::max(m, x);
}
return m;
}
bool empty() const {
return q.empty();
}
private:
std::deque<int> q;
};
// Optimal (Efficient) Solution
// Uses a normal queue (deque) along with an auxiliary deque to keep track of
// potential maximum values. Each operation is O(1) amortized.
class OptimalMaxQueue {
public:
void push(int x) {
q.push_back(x);
// Remove elements smaller than x from the back of maxDeque.
while (!maxDeque.empty() && maxDeque.back() < x) {
maxDeque.pop_back();
}
maxDeque.push_back(x);
}
void pop() {
if (q.empty())
return;
int frontVal = q.front();
q.pop_front();
if (frontVal == maxDeque.front()) {
maxDeque.pop_front();
}
}
int max() const {
assert(!q.empty());
return maxDeque.front();
}
bool empty() const {
return q.empty();
}
private:
std::deque<int> q;
std::deque<int> maxDeque;
};
// Alternative (Educational) Solution
// Uses a deque for the queue and a multiset to keep elements in sorted order.
// Each push and pop operation requires updating the multiset, achieving O(log n)
// per operation.
class AlternativeMaxQueue {
public:
void push(int x) {
q.push_back(x);
mset.insert(x);
}
void pop() {
if (q.empty())
return;
int frontVal = q.front();
q.pop_front();
auto it = mset.find(frontVal);
if (it != mset.end()) {
mset.erase(it);
}
}
int max() const {
assert(!q.empty());
return *mset.rbegin();
}
bool empty() const {
return q.empty();
}
private:
std::deque<int> q;
std::multiset<int> mset;
};
// Test cases for correctness
void test() {
// Simulate a series of operations:
// push 3, push 1, push 5, max, pop, max
std::vector<int> expected_max_values = {5, 5};
// Test SimpleMaxQueue
{
SimpleMaxQueue mq;
mq.push(3);
mq.push(1);
mq.push(5);
int m1 = mq.max();
mq.pop();
int m2 = mq.max();
assert(m1 == expected_max_values[0]);
assert(m2 == expected_max_values[1]);
}
// Test OptimalMaxQueue
{
OptimalMaxQueue mq;
mq.push(3);
mq.push(1);
mq.push(5);
int m1 = mq.max();
mq.pop();
int m2 = mq.max();
assert(m1 == expected_max_values[0]);
assert(m2 == expected_max_values[1]);
}
// Test AlternativeMaxQueue
{
AlternativeMaxQueue mq;
mq.push(3);
mq.push(1);
mq.push(5);
int m1 = mq.max();
mq.pop();
int m2 = mq.max();
assert(m1 == expected_max_values[0]);
assert(m2 == expected_max_values[1]);
}
std::cout << "All tests passed!\n";
}
int main() {
test();
return 0;
}