{"id":414,"date":"2023-01-26T11:54:04","date_gmt":"2023-01-26T11:54:04","guid":{"rendered":"https:\/\/premsvmm.com\/?p=414"},"modified":"2023-01-27T17:35:54","modified_gmt":"2023-01-27T17:35:54","slug":"implement-stack","status":"publish","type":"post","link":"https:\/\/premsvmm.com\/index.php\/2023\/01\/26\/implement-stack\/","title":{"rendered":"Implement Stack"},"content":{"rendered":"\n<pre class=\"wp-block-preformatted\">1. Implement the stack\n2. Get Min value in stack with o(1)\n3. Improve space complexity as well <div class=\"open_grepper_editor\" title=\"Edit &amp; Save To Grepper\"><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Code<\/h5>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;eclipse&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Java&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;java&quot;}\">import java.util.ArrayDeque;\nimport java.util.Deque;\n\ninterface IntStack {\n    void push(int i);\n\n    int pop();\n\n    int peek();\n\n    boolean empty();\n}\n\nclass IntStackImpl implements IntStack {\n    private Deque&lt;Integer&gt; dq;\n    protected Deque&lt;Integer&gt; minStack;\n\n    public IntStackImpl() {\n        dq = new ArrayDeque&lt;Integer&gt;();\n        minStack = new ArrayDeque&lt;Integer&gt;();\n    }\n\n    public boolean empty() {\n        return dq.isEmpty();\n    }\n\n    public void push(int i) {\n        dq.push(i);\n        if (minStack.isEmpty()) {\n            minStack.push(i);\n        } else {\n            int topValue = minStack.peek();\n            if (i &lt;= topValue) {\n                minStack.push(i);\n            }\n        }\n    }\n\n    public int pop() {\n        int value = dq.pop();\n        if (minStack.peek() == value) {\n            minStack.pop();\n        }\n        return value;\n    }\n\n    public int peek() {\n        return dq.peek();\n    }\n}\n\ninterface MinStack extends IntStack {\n    int min(); \/\/ gives minimum value without changing stack\n}\n\nclass MinStackImpl extends IntStackImpl implements MinStack {\n\n    \/\/ implement this\n    public int min() {\n        return minStack.peek();\n    }\n}\n\nclass Solution {\n    public static void main(String[] args) {\n        MinStack ms = new MinStackImpl();\n        ms.push(5);\n        System.out.println(ms.min()); \/\/should print: 5\n        ms.push(1);\n        ms.push(3);\n        System.out.println(ms.min()); \/\/should print: 1\n        System.out.println(ms.pop()); \/\/should print: 3\n        System.out.println(ms.min()); \/\/should print: 1\n        System.out.println(ms.pop()); \/\/should print: 1\n        System.out.println(ms.min()); \/\/should print: 5\n    }\n}<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>1. Implement the stack 2. Get Min value in stack with o(1) 3. Improve space complexity as well Code<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-global-header-display":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","footnotes":""},"categories":[10],"tags":[11],"_links":{"self":[{"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/posts\/414"}],"collection":[{"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/comments?post=414"}],"version-history":[{"count":2,"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/posts\/414\/revisions"}],"predecessor-version":[{"id":416,"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/posts\/414\/revisions\/416"}],"wp:attachment":[{"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/media?parent=414"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/categories?post=414"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/tags?post=414"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}