We define the variable minEle, which stores the current minimum element on the stack. Now the interesting part is how to handle the case when the minimum element is removed. To handle this, we push "2x - minEle" onto the stack instead of x so that the previous minimum element can be obtained using the current minEle and its value stored on the stack. Below are detailed steps and explanations for the work.
Push (x): Inserts x at the top of the stack .
If stack is empty, insert x into the stack and make minEle equal to x. If stack is not empty, compare x with minEle. Two cases arise: If x is greater than or equal to minEle, simply insert x. If x is less than minEle, insert (2*x โ minEle) into the stack and make minEle equal to x. For example, let previous minEle was 3. Now we want to insert 2. We update minEle as 2 and insert 2*2 โ 3 = 1 into the stack.
Pop (): removes an element from the top of the stack.
Remove element from top. Let the removed element be y. Two cases arise: If y is greater than or equal to minEle, the minimum element in the stack is still minEle. If y is less than minEle, the minimum element now becomes (2*minEle โ y), so update (minEle = 2*minEle โ y). This is where we retrieve previous minimum from current minimum and its value in stack. For example, let the element to be removed be 1 and minEle be 2. We remove 1 and update minEle as 2*2 โ 1 = 3. / Java program to implement a stack that supports // getMinimum() in O(1) time and O(1) extra space. import java.util.*; // A user defined stack that supports getMin() in // addition to push() and pop() class MyStack { Stack<Integer> s; Integer minEle; // Constructor MyStack() { s = new Stack<Integer>(); } // Prints minimum element of MyStack void getMin() { // Get the minimum number in the entire stack if (s.isEmpty()) System.out.println("Stack is empty"); // variable minEle stores the minimum element // in the stack. else System.out.println("Minimum Element in the " + " stack is: " + minEle); } // prints top element of MyStack void peek() { if (s.isEmpty()) { System.out.println("Stack is empty "); return; } Integer t = s.peek(); // Top element. System.out.print("Top Most Element is: "); // If t < minEle means minEle stores // value of t. if (t < minEle) System.out.println(minEle); else System.out.println(t); } // Removes the top element from MyStack void pop() { if (s.isEmpty()) { System.out.println("Stack is empty"); return; } System.out.print("Top Most Element Removed: "); Integer t = s.pop(); // Minimum will change as the minimum element // of the stack is being removed. if (t < minEle) { System.out.println(minEle); minEle = 2*minEle - t; } else System.out.println(t); } // Insert new number into MyStack void push(Integer x) { if (s.isEmpty()) { minEle = x; s.push(x); System.out.println("Number Inserted: " + x); return; } // If new number is less than original minEle if (x < minEle) { s.push(2*x - minEle); minEle = x; } else s.push(x); System.out.println("Number Inserted: " + x); } }; // Driver Code public class Main { public static void main(String[] args) { MyStack s = new MyStack(); s.push(3); s.push(5); s.getMin(); s.push(2); s.push(1); s.getMin(); s.pop(); s.getMin(); s.pop(); s.peek(); } }
How does this approach work?
When the inserted element is less than minEle, we insert "2x - minEle". It is important to note that 2x - minEle will always be less than x (proved below), i.e. The new minEle and, choosing this element, we will see that something unusual happened, because the popped element is smaller than minEle. So we will update minEle
How is 2 * x - minEle less than x in push ()?
x <minEle, which means x - minEle <0
// Add x on both sides
x - minEle + x <0 + x
2 * x - minEle <x
We can conclude 2 * x - minEle <new minEle
If you find that element (y) is smaller than the current minEle, we will find a new minEle = 2 * minEle - y.
Like the previous minimum element, prevMinEle is, 2 * minEle - y?
in pop () is a popped item?
// We hit y like 2x - prevMinEle. Here
// prevMinEle is the minEle before inserting y
y = 2 * x - prevMinEle
// The value of minEle was equal to x minEle = x.
new minEle = 2 * minEle - y
= 2*x - (2*x - prevMinEle) = prevMinEle // This is what we wanted