Min stack

Problem: [https://leetcode.com/problems/min-stack/ 155. Min Stack]

Problem description
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
 *   – Push element  onto stack.
 *   – Removes the element on top of the stack.
 *   – Get the top element.
 *   – Retrieve the minimum element in the stack.

Example 1
["MinStack","push","push","push","getMin","pop","top","getMin"] ],[-2],[0],[-3],[],[],[],[
 * Input

[null,null,null,null,-3,null,0,-2]
 * Output


 * Explanation

Constraints

 * Methods,   and   operations will always be called on non-empty stacks.

Data structure

 * Keep a data stack to record all the data.
 * Keep a min stack to record the min values pushed into the stack.

Push
When we push a new element, we compare this with the current minimum value on the min stack.


 * Corner case: If the stack is empty, then we do not need to compare, but just push one element to both the data stack and the min stack.
 * Multiple instances of min: For the purpose of keeping multiple instances of min, every time when we push an element onto the data stack, we push the minimum value to the min stack.
 * For example: If data is 5 → 4 → 1 → 1 → 2 → 2 → 1 → -1 → -1， then the min stack is 5 → 4 → 1 → 1 → 1 → 1 → 1 → -1 → -1.


 * Time complexity: O(1), since we do the push on the stack once for each, each with time complexity O(1).


 * Space complexity: The space complexity for storing the data is O(n), since we use exactly twice as much space as we have for the data.

Pop
We obtain access to the first element to pop off with the current minimum value on the min stack.


 * Corner case: If the stack is empty, then we do not need to do anything.

Then we can pop from the top of the data stack as well as the min stack.


 * Time complexity: O(1), since we do the pop on the stack once for each, each with time complexity O(1).

Get minimum value

 * Time complexity: This can be done in O(1) time complexity as we get the top (minimum value) of the min stack.

Data structure
Similar to approach 1, we can use a data stack and a min stack.

But this time each element in the min stack is a pair:
 * : the minimum value.
 * : a counter to record how many times that minimum value has been pushed into the data stack.

Push

 * Should the minimum value be updated, we add a new entry to the min stack, and set its count to 1.
 * If the minimum value does not change, add the count by 1.


 * Time complexity: O(1), since we take constant time for both pushing to the data stack and updating the min stack.


 * Space complexity: O(n) for both the data stack and the min stack.

Pop

 * If the count of the current min value on the min stack is 1, then this entry is deleted.
 * Otherwise, we just decrease the counter by 1.


 * Time complexity: O(1), since we take constant time for both popping from the data stack and updating the min stack.

Get minimum value

 * Time complexity: Again, this can be done in O(1) time complexity as we get the top (minimum value) of the min stack.

Data structure
The data structure is similar to approach 1A: a data stack and a min stack.

But we make some changes to the min stack:
 * : the minimum value.
 * : a position at which this minimum value occurred first.

The first element on the min stack has its  value 0, corresponding to the index 0 on the data stack.

Push

 * Whenever we get a new minimum value, we record the position of this element that first appears on the stack.
 * If we are not inserting a new minimum value (e.g. inserting 2 when the minimum is 1), we do not do anything on the min stack.


 * Time complexity: O(1) for the push actions on both the data stack and the min stack.


 * Space complexity: O(n) for both the data stack and the min stack.

Pop
We compare the index of the element to be popped (equals size of stack minus 1) and the  field of the min value on the min stack.
 * If the index of this element equals the  field of the min value on the min stack, then pop the entry from the min stack.
 * Otherwise, we do not do anything on the min stack


 * Time complexity: O(1) for the pop actions on both the data stack and the min stack.

Get minimum value

 * Time complexity: Again, this can be done in O(1) time complexity as we get the top (minimum value) of the min stack.

C++

 * Stack APIs:

Java

 * The  class: Class