# Pushby Isai Damier, Android Engineer @ Google

```/**************************************************************************
* Author: Isai Damier
* Title: Stack With Get-Min
* Project: geekviewpoint
* Package: datastructure
*
* Description: Normally, only the top of a stack is available for
*    peeking and popping. Here, however, the requirement is to make
*    the minimum value on the stack always viewable in O(1).
*    To that end, in addition to the primary stack of operation
*    (called primary), a separate stack (called minimums) must be
*    used to track the minimum value on the primary stack. The point
*    is not to pop the minimum value in O(1); that would require a
*    bit more work. Rather, the getMinimum function allows peeking
*    into the stack, as it were, to see the minimum value on the
*    stack.
*
*    Throughout the implementation it should be clear that the
*    primary stack is the real (i.e. representative) stack and that
*    the minimums stack is an auxiliary stack that exists only so
*    the getMinimum() function may exist.
*************************************************************************/
import java.util.Stack;

public class StackWithGetMin {

/**
*  Stack<Integer> primary: where all the elements actually reside.
*/
private final Stack<Integer> primary = new Stack<Integer>();
/**
*  Stack<Integer> minimums: used to track the minimum values on
*  the stack. The most minimum value is always on top.
*/
private final Stack<Integer> minimums = new Stack<Integer>();

/**********************************************************************
* Statement:
*   Add the given element to the stack.
*
* Time Complexity of Solution:
*   Best = Average = Worst = const.
*
* Technical Details: If the stack is empty, then the given element
*     is perforce also the minimum element on the stack. Therefore
*     mark the element as minimum by also pushing it into the
*     minimums stack. On the other hand, if the stack (i.e. the
*     primary stack) is not empty, check if the new element is less
*     than the current minimum element. If it is, make the new
*     element the new minimum.
*
*     The explanation is slightly different from the actual code
*     to give the reader a different viewpoint.
*
**********************************************************************/
public void push(int e) {
if (primary.isEmpty() || e < minimums.peek()) {
minimums.push(e);
}
primary.push(e);
}
}```
```import org.junit.Test;
import static org.junit.Assert.*;

public class StackWithGetMinTest {

/**
* Test of push method, of class StackWithGetMin.
*/
@Test
public void testPush() {
System.out.println("push");
int e = 0;
StackWithGetMin stack = new StackWithGetMin();
Integer[] inputs = {40, 65, 5, 6, 32, 4, 7, 1, 2, 84, 9, 10};
for (int i : inputs) {
stack.push(i);
}
assertFalse(stack.isEmpty());
assertEquals(inputs.length, stack.size());
}
}```