package com.fishercoder.solutions;


import java.util.Stack;

/**
 * 155. Min Stack
 * Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

 * push(x) -- Push element x onto stack.
 * pop() -- Removes the element on top of the stack.
 * top() -- Get the top element.
 * getMin() -- Retrieve the minimum element in the stack.

 * Example:
 * MinStack minStack = new MinStack();
 * minStack.push(-2);
 * minStack.push(0);
 * minStack.push(-3);
 * minStack.getMin();   --> Returns -3.
 * minStack.pop();
 * minStack.top();      --> Returns 0.
 * minStack.getMin();   --> Returns -2.
 */

public class _155 {

    public static class MinStack {
        private Stack<Integer> stack;
        private int min;

        /**
         * initialize your data structure here.
         */
        public MinStack() {
            stack = new Stack();
            min = Integer.MAX_VALUE;
        }

        public void push(int x) {
            /**All the trick happens here, we push the second minimum number onto the stack before we push the newer one,
             * this way, when popping, we could always get the next minimum one in constant time.*/
            if (x <= min) {
                stack.push(min);
                min = x;
            }
            stack.push(x);
        }

        public void pop() {
            if (min == stack.peek()) {
                stack.pop();
                min = stack.pop();
            } else {
                stack.pop();
            }
            if (stack.isEmpty()) {
                min = Integer.MAX_VALUE;
            }
        }

        public int top() {
            return stack.peek();
        }

        public int getMin() {
            return min;
        }
    }

}

@@ -29,17 +29,16 @@
 tions;%0A%0A
-%0A
 import j
@@ -668,16 +668,42 @@
 c class 
+Solution1 %7B%0A        class 
 MinStack
@@ -705,16 +705,20 @@
 Stack %7B%0A
+    
         
@@ -747,32 +747,36 @@
  stack;%0A        
+   
+ 
 private int min;
@@ -785,20 +785,28 @@
         
-/**%0A
+    /**%0A    
         
@@ -854,19 +854,27 @@
         
+  
+  
 */%0A
+    
         
@@ -901,24 +901,28 @@
             
+    
 stack = new 
@@ -934,32 +934,36 @@
 ();%0A            
+ 
+   
 min = Integer.MA
@@ -971,35 +971,43 @@
 _VALUE;%0A        
-%7D%0A%0A
+    %7D%0A%0A    
         public v
@@ -1036,16 +1036,20 @@
         
+    
 /**All t
@@ -1163,16 +1163,20 @@
         
+    
 * this w
@@ -1262,24 +1262,28 @@
             
+   
+ 
 if (x %3C= min
@@ -1282,24 +1282,28 @@
 x %3C= min) %7B%0A
+    
             
@@ -1335,24 +1335,28 @@
             
+    
 min = x;%0A   
@@ -1356,34 +1356,42 @@
  x;%0A            
+  
-%7D%0A
+  %7D%0A    
             stac
@@ -1401,32 +1401,36 @@
 ush(x);%0A        
+    
 %7D%0A%0A        publi
@@ -1416,32 +1416,36 @@
      %7D%0A%0A        
+ 
+   
 public void pop(
@@ -1444,24 +1444,28 @@
 oid pop() %7B%0A
+    
             
@@ -1487,24 +1487,28 @@
 k.peek()) %7B%0A
+    
             
@@ -1536,24 +1536,28 @@
             
+    
 min = stack.
@@ -1575,16 +1575,20 @@
         
+    
 %7D else %7B
@@ -1596,32 +1596,36 @@
                 
+   
+ 
 stack.pop();%0A   
@@ -1625,32 +1625,36 @@
 ();%0A            
+    
 %7D%0A            if
@@ -1647,24 +1647,28 @@
             
+ 
+   
 if (stack.is
@@ -1686,32 +1686,36 @@
                 
+  
+  
 min = Integer.MA
@@ -1727,34 +1727,42 @@
 UE;%0A            
-%7D%0A
+    %7D%0A    
         %7D%0A%0A     
@@ -1756,32 +1756,36 @@
      %7D%0A%0A        
+ 
+   
 public int top()
@@ -1783,24 +1783,28 @@
 int top() %7B%0A
+    
             
@@ -1820,24 +1820,28 @@
 ack.peek();%0A
+    
         %7D%0A%0A 
@@ -1839,32 +1839,36 @@
      %7D%0A%0A        
+    
 public int getMi
@@ -1881,24 +1881,28 @@
             
+    
 return min;%0A
@@ -1913,15 +1913,28 @@
     
+   
+ 
 %7D%0A
+    
     %7D%0A
+    %7D
 %0A%7D%0A

refactor 155