Skip to content

Commit 3fcf2fc

Browse files
authored
Create maxQueue-using-deque.cpp
max-queue using deque (which can be done with 2 stack also)
1 parent d1461c8 commit 3fcf2fc

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// submission link: https://leetcode.com/problems/sliding-window-maximum/
2+
3+
4+
struct maxQueue {
5+
deque<pair<int, int>> q; // we maintain decreasing order
6+
int _size;
7+
maxQueue() {
8+
_size = 0;
9+
}
10+
void push(int num) {
11+
int inFront = 0;
12+
while (!q.empty() && q.back().first < num) {
13+
inFront += q.back().second + 1; // previous_deletions + curent_deletions
14+
q.pop_back();
15+
}
16+
q.push_back({num, inFront});
17+
_size += 1;
18+
}
19+
20+
int max() {
21+
return q.front().first;
22+
}
23+
24+
void pop() {
25+
int num = q.front().first;
26+
int inFront = q.front().second;
27+
q.pop_front();
28+
if (inFront) {
29+
inFront -= 1;
30+
q.push_front({num, inFront});
31+
}
32+
_size -= 1;
33+
}
34+
35+
int size() {
36+
return _size;
37+
}
38+
};

0 commit comments

Comments
 (0)