Java Beats 100% Space. Simple Java Two Stack Solution
Time complexity: O(1). Operations done in O(1) time.\n\n- Space complexity: O(2N). Two stacks are used to contain values.\n\n# Code\n\nclass MinStack {\n\n    /
- Time complexity: O(1). Operations done in O(1) time.\n\n- Space complexity: O(2N). Two stacks are used to contain values.\n\n# Code\n```\nclass MinStack {\n\n    //Using two stacks to store the values.\n    Stack<Integer> pushStack = new Stack<>(); //Stack to store all the values pushed\n    Stack<Integer> minStack = new Stack<>();  //Stack to Store all the minimum values\n    \n    //Leaving constructor empty\n    public MinStack() {}\n    \n\n    public void push(int val) {\n\n        /*\n        \n            If it is the first element or \n            //The is the new element (val) that we are pushing, we check if it is smaller than our\n            Current element in the minStack.\n\n            For Example: [-1, 1, -2, 3]. And Stack has -1 in it as Minimum and we are at index 2 now\n                        0  1   2  3\n                                |\n                                |\n                            Here Now\n\n                |______|          \n                |______|   ----> checking is current val <= -1\n                |__-1__|                          -2    <= -1 \n                                                    True.\n            Stack as of now\n\n            So we add -2 to the stack\n\n                |______|          \n                |__-2__|  \n                |__-1__|\n            \n            If you are wondering why we have used \'<=\' equals sign here, \n            thas because, for cases like: [-1,2,-2,-2,-2,1], if we don\'t add all the similar\n            values, then when we pop from the stack we will lose the element.\n\n        */\n        pushStack.push(val);\n        if(minStack.isEmpty() || pushStack.peek() <= minStack.peek())\n            minStack.push(val);\n        \n        //We push all the values to pushStack without any conditions\n        \n        \n    }\n    \n    public void pop() {\n\n        //if element to pop is also our current min then we also need to pop it\n        //Example : [-1,2,3,-4] --> Current Min : -4. if we need to. pop from the stack\n        // we will remove -4. and our dataset will remain to be [-1,2,3].\n        //So as we do not have -4 anymore we also pop it from the min stack.\n        if(pushStack.peek().equals(minStack.peek()))\n        {\n\n            minStack.pop();\n        }\n\n        //Normal pop from the values stack\n        pushStack.pop();\n    }\n    \n    //For top we check in push Stack\n    public int top() {\n        return pushStack.peek();\n    }\n    \n    //For peek we check in min Stack\n    public int getMin() {\n        return minStack.peek();\n    }\n}\n\n/**\n * Your MinStack object will be instantiated and called as such:\n * MinStack obj = new MinStack();\n * obj.push(val);\n * obj.pop();\n * int param_3 = obj.top();\n * int param_4 = obj.getMin();\n */\n```\nHave Patience. Keep Coding :-)
['Java']