Implementation of Minimum Queue
Example 1:
Input: push={5,3,10} ,getminimum ,push={2,4}, getminimum, pop, getminimum
Output: 3, 2, 2
Approach:
Java
import java.util.Stack;import javafx.util.Pair;@SuppressWarnings("restriction")public class MinimumQueue {public static void main(String[] args) {// stack pair to hold the element and the// minimum at the top of stackStack<Pair<Integer, Integer>> stack1 =new Stack<Pair<Integer, Integer>>();Stack<Pair<Integer, Integer>> stack2 =new Stack<Pair<Integer, Integer>>();// add element'saddELement(stack1, 5);addELement(stack1, 23);addELement(stack1, 10);// find minimum elementSystem.out.print(findMinimumElement(stack1, stack2) + " ");// add again element'saddELement(stack1, 3);addELement(stack1, 6);// find minimumSystem.out.print(findMinimumElement(stack1, stack2) + " ");removeElement(stack1, stack2);// find minimumSystem.out.print(findMinimumElement(stack1, stack2) + " ");}private static void removeElement(Stack<Pair<Integer, Integer>>stack1, Stack<Pair<Integer, Integer>> stack2) {// if second is emptyif (stack2.isEmpty()) {// this means convert the stack into queuewhile (!stack1.isEmpty()) {int element = stack1.pop().getValue();int minimum;// if second is empty// minimum is the current elementif (stack2.isEmpty())minimum = element;// else the minimum is min of current// element and stack topelseminimum = Math.min(element, stack2.peek().getValue());// push into the new stackstack2.add(new Pair<Integer, Integer>(element, minimum));}}// if second stack is not empty then pop the top elementif (!stack2.isEmpty())stack2.pop();}private static int findMinimumElement(Stack<Pair<Integer,Integer>> stack1, Stack<Pair<Integer, Integer>> stack2) {// if both stack is empty then -1if (stack1.isEmpty() && stack2.isEmpty())return -1;// if stack1 is empty then// top of stack2else if (stack1.isEmpty()) {return stack2.peek().getValue();// if stack2 is empty then// top of stack1} else if (stack2.isEmpty()) {return stack1.peek().getValue();// find min of both stack1, stack2} else {return Math.min(stack2.peek().getValue(),stack1.peek().getValue());}}private static void addELement(Stack<Pair<Integer, Integer>>stack1, int value) {int min = 0;// if stack is empty then// the minimum element is the given elememtif (stack1.isEmpty())min = value;// else the minimum element is// the min of stack top and current elementelse {min = Math.min(stack1.peek().getValue(), value);}// push the current element// with the minimum elementstack1.add(new Pair<Integer, Integer>(value, min));}}
C++
#include <bits/stdc++.h>using namespace std;//function to add the new elementvoid addElement(stack<pair<int,int>> &st1,int value){int minimum;//if stack is empty//then minimum element is the//new elementif(st1.empty())minimum=value;//else the minimum element//is the min of stack top and current//elementelseminimum=min(value,st1.top().second);//push the new element into the stackst1.push({value,minimum});}//function to find the minimum element//of the queueint findMinimumElement(stack<pair<int,int>> &st1,stack<pair<int,int>> &st2){//if both are empty return -1if(st1.empty()&&st2.empty())return -1;//if fisrt is empty return//second top elementelse if(st1.empty())return st2.top().second;//if second is empty return//fisrt top elementelse if(st2.empty())return st1.top().second;//else return min of both//stackselsereturn min(st1.top().second,st2.top().second);}//function to remove the elementvoid removeElement(stack<pair<int,int>> &st1,stack<pair<int,int>> &st2){//if second is emptyif(st2.empty()){//this means convert the stack into//queuewhile(!st1.empty()){int element=st1.top().first;st1.pop();int minimum;//if second is empty//minimum is the current elementif(st2.empty())minimum=element;//else the minimum//is min of current element and//stack topelseminimum=min(element,st2.top().second);//push into the new stackst2.push({element,minimum});}}//if second stack is not//empty then pop the top//elementif(!st2.empty())st2.pop();}int main(){stack<pair<int,int>> st1,st2;addElement(st1,5);addElement(st1,3);addElement(st1,10);//print 3cout<<findMinimumElement(st1,st2)<<"\n";addElement(st1,2);addElement(st1,4);//print 2cout<<findMinimumElement(st1,st2)<<"\n";removeElement(st1,st2);//print 2cout<<findMinimumElement(st1,st2)<<"\n";return 0;}
No comments:
Post a Comment