Let's create a minimum queue as well:
We'll do this by simulating a queue using two stacks.
We add new elements to the stack s1, and remove elements from s2. If s2 is empty, we move all elements from s1 to s2, and pop the top.
<pair<int, int>> s1, s2;
stack
// Find the minimum:
if (s1.empty() || s2.empty()) {
= s1.empty() ? s2.top().second : s1.top().second;
minimum } else {
= min(s1.top().second, s2.top().second);
minimum }
// Add an element:
int minimum = s1.empty() ? new_element : min(new_element, s1.top().second);
.push({new_element, minimum});
s1
// Removing an element:
if (s2.empty()) {
while (!s1.empty()) {
int element = s1.top().first;
.pop();
s1int minimum = s2.empty() ? element : min(element, s2.top().second);
.push({element, minimum});
s2}
}
int remove_element = s2.top().first;
.pop(); s2