Min Stack  -  Easy Explaination
\n# Approach and Code WalkThrough\n Describe your approach to solving the problem. \n\nThe approach is quite easy . We are going to maintain 2 stacks for this q
\n# Approach and Code WalkThrough\n<!-- Describe your approach to solving the problem. -->\n\nThe approach is quite easy . We are going to maintain 2 stacks for this question . One stack is going to be the normal one , in which we normally push , pop and peek from.\n\nSecond stack is going to maintain the minimum element from the main stack . \n\nLets discuss all the methods asked in the question.\n\nFirst , we declare the two stacks in:\n\n```\nprivate Stack<Integer>stack;\nprivate Stack<Integer>minStack;\n```\n1. **public minStack()** : This is a constructor that is being used to initialize the stacks .\n\n```\npublic minStack()\n{   \nstack = new Stack<>();\nminStack = new Stack<>();\n\n}\n```\n2. **public void push(int val)** : This method pushes value into the stack . The crunch here is that the values are pushed into the **minStack** only if the minStack is empty or if the value to be pushed is smaller than the element present at the top of the minStack.\n \n**Why?** : because the minStack only needs to hold the minimum values , so if gets a value lesser than the one already present in the minStack , it pushes it .\nHere\'s the relevant part of the code : \n \n```\npublic void push(int val)\n{\n     stack.push(val);\n    \nif(minStack.isEmpty() || val<=minStack.peek())\n{\n     minStack.push(val);\n}\n\n}\n```\n3. **public void pop()** : This method removes the element from the stacks but again with a twist .The method first checks if the minStack has the same element at the top as the main stack . If it is , then only element is removed from minstack first and then the main stack.\n\n**Why?** : because the elements should be synchronized in both the stacks. It should not happen that the minStack has a value in it when the main stack doesn\'t have that value in it as the minStack is kind of a sub-stack of the main stack that holds the minimum element from the stack. The minStack was designed to hold minimum element from **The main Stack**\n\n ```\npublic void pop() {\n         if(stack.peek().equals(minStack.peek()))\n         {\n            minStack.pop();\n         }\n     stack.pop();\n\n    }\n```\n\n4. **public int top()** : This method is used to access the topmost element of the stack.\n\n```\npublic int top() {\n          \n        return  stack.peek();\n\n\n    }\n \n```\n\n5. **public int getMin()** : gives the smallest numbers that the minStack holds.\n\n```\npublic int getMin() {\n        return minStack.peek();\n    }\n```\n---\n\n# Complexity\n- Time complexity : $$O(1)$$\n<!-- Add your time complexity here, e.g.  -->\n\n- Space complexity : $$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass MinStack {\n\n    private Stack<Integer>stack;\n    private Stack<Integer>minStack;\n\n    public MinStack() {\n        stack = new Stack<>();\n        minStack = new Stack<>();\n    }\n    \n    public void push(int val) {\n        stack.push(val);\n        if(minStack.isEmpty() || val<=minStack.peek())\n        {\n            minStack.push(val);\n        }\n    }\n    \n    public void pop() {\n         if(stack.peek().equals(minStack.peek()))\n         {\n            minStack.pop();\n         }\n     stack.pop();\n\n    }\n    \n    public int top() {\n          \n        return  stack.peek();\n\n\n    }\n    \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```\n\n---\n**Feel free to discuss the problem in comments and do upvote**\n**ThankYou**
['Stack', 'Java']