-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminQueue.cpp
101 lines (89 loc) · 2.33 KB
/
minQueue.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
#include <bits/stdc++.h>
/*
Minimum Queue Implementation
Queue = add elements at the end and remove them from the front
On average all operations take O(1) Time, since each element is pushed, popped (or moved) only once.
*/
/*
This implementation uses a double ended queue, a deque, and does not store all elements.
The queue only stores elements needed to calculate the minimum.
*/
class MinimumQueue1
{
public:
MinimumQueue1() = default;
std::deque<int> dq;
void push(int x)
{
while (!dq.empty() && dq.back() < x)
{
dq.pop_back();
}
dq.push_back(x);
}
void remove(int x)
{
if (!dq.empty() && dq.front() == x)
dq.pop_front();
}
int getMin()
{
return dq.front();
}
};
/*
This implementation uses 2 stacks.
Each stack element will contain the value at that index, and minimum upto that index when it was pushed.
When inserting an element, it is pushed into pushStk. Popping of an element is done from popStk.
When popStk is empty, we remove elements from pushStk and push them into popStk (which reverses the order).
Similar idea can be used to solve Leetcode 155 (https://leetcode.com/problems/min-stack/)
*/
class MinimumQueue2
{
private:
void reogranize(std::stack<std::pair<int, int>> &pushStk, std::stack<std::pair<int, int>> &popStk)
{
while (!pushStk.empty())
{
popStk.push(pushStk.top());
pushStk.pop();
}
}
public:
MinimumQueue2() = default;
std::stack<std::pair<int, int>> pushStk, popStk;
void push(int x)
{
if (pushStk.empty())
{
pushStk.push(std::make_pair(x, x));
}
else
{
int minVal = std::min(pushStk.top().second, x);
pushStk.push(std::make_pair(x, minVal));
}
}
int pop()
{
if (popStk.empty())
{
reogranize(pushStk, popStk);
}
int val = popStk.top().first;
popStk.pop();
return val;
}
int getMin()
{
if (popStk.empty() || pushStk.empty())
{
return pushStk.empty() ? popStk.top().second : pushStk.top().second;
}
return std::min(pushStk.top().second, popStk.top().second);
}
};
int main()
{
return 0;
}