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 Solution1 {
        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 the value on the top of the stack happens to be the current minimum, we'll pop twice and change
                 * the current min value to be the last min value */
                if (min == stack.pop()) {
                    min = stack.pop();
                }
            }

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

            public int getMin() {
                return min;
            }
        }
    }
}

@@ -50,583 +50,67 @@
 til.
-Stack;%0A%0A/**%0A * 155. Min Stack%0A * Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.%0A%0A * push(x) -- Push element x onto stack.%0A * pop() -- Removes the element on top of the stack.%0A * top() -- Get the top element.%0A * getMin() -- Retrieve the minimum element in the stack.%0A%0A * Example:%0A * MinStack minStack = new MinStack();%0A * minStack.push(-2);%0A * minStack.push(0);%0A * minStack.push(-3);%0A * minStack.getMin();   --%3E Returns -3.%0A * minStack.pop();%0A * minStack.top();      --%3E Returns 0.%0A * minStack.getMin();   --%3E Returns -2.%0A */
+Deque;%0Aimport java.util.LinkedList;%0Aimport java.util.Stack;
 %0A%0Apu
@@ -265,92 +265,8 @@
 n;%0A%0A
-            /**%0A             * initialize your data structure here.%0A             */%0A
     
@@ -1300,32 +1300,32 @@
 ;%0A            %7D%0A
-
         %7D%0A    %7D%0A
@@ -1323,11 +1323,1213 @@
  %7D%0A    %7D
+%0A%0A    public static class Solution2 %7B%0A        /**%0A         * We could store a pair onto the stack: the first element in the pair is the value itself,%0A         * the second element in the pair is the current minimum element so far seen on the stack.%0A         */%0A        class MinStack %7B%0A            Deque%3Cint%5B%5D%3E stack;%0A%0A            public MinStack() %7B%0A                stack = new LinkedList%3C%3E();%0A            %7D%0A%0A            public void push(int val) %7B%0A                if (!stack.isEmpty()) %7B%0A                    int%5B%5D last = stack.peekLast();%0A                    int currentMin = last%5B1%5D;%0A                    if (val %3C currentMin) %7B%0A                        stack.addLast(new int%5B%5D%7Bval, val%7D);%0A                    %7D else %7B%0A                        stack.addLast(new int%5B%5D%7Bval, currentMin%7D);%0A                    %7D%0A                %7D else %7B%0A                    stack.addLast(new int%5B%5D%7Bval, val%7D);%0A                %7D%0A            %7D%0A%0A            public void pop() %7B%0A                stack.pollLast();%0A            %7D%0A%0A            public int top() %7B%0A                return stack.peekLast()%5B0%5D;%0A            %7D%0A%0A            public int getMin() %7B%0A                return stack.peekLast()%5B1%5D;%0A            %7D%0A        %7D%0A%0A    %7D
 %0A%7D%0A

add a solution for 155