{"id":22513470,"url":"https://github.com/emahtab/min-stack","last_synced_at":"2026-03-19T23:03:04.773Z","repository":{"id":79525664,"uuid":"242058728","full_name":"eMahtab/min-stack","owner":"eMahtab","description":"Min Stack","archived":false,"fork":false,"pushed_at":"2024-10-16T16:24:34.000Z","size":26,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-06-03T23:59:58.334Z","etag":null,"topics":["constant-time","leetcode","problem-solving","stack"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eMahtab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-02-21T05:04:56.000Z","updated_at":"2024-10-16T16:24:37.000Z","dependencies_parsed_at":"2025-06-02T22:01:55.343Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/min-stack","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/eMahtab/min-stack","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fmin-stack","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fmin-stack/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fmin-stack/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fmin-stack/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/min-stack/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fmin-stack/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28819764,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-27T18:44:20.126Z","status":"ssl_error","status_checked_at":"2026-01-27T18:44:09.161Z","response_time":168,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["constant-time","leetcode","problem-solving","stack"],"created_at":"2024-12-07T03:12:26.355Z","updated_at":"2026-01-27T19:45:21.434Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Min Stack\n## https://leetcode.com/problems/min-stack\n\nDesign a stack that supports push, pop, top, and retrieving the minimum element in constant time.\n\n1. push(x) -- Push element x onto stack.\n2. pop() -- Removes the element on top of the stack.\n3. top() -- Get the top element.\n4. getMin() -- Retrieve the minimum element in the stack.\n \n```\nExample:\n\nMinStack minStack = new MinStack();\nminStack.push(-2);\nminStack.push(0);\nminStack.push(-3);\nminStack.getMin();   --\u003e Returns -3.\nminStack.pop();\nminStack.top();      --\u003e Returns 0.\nminStack.getMin();   --\u003e Returns -2.\n```\n## Approach :\nThe question asks us to implement 4 functions and all of them should have constant runtime, which means no matter how large the input is, those 4 functions should always run in constant time.\n\nNow we already know that, a Stack gives us `push(x)` , `pop()` and `peek()` functions, all of these 3 functions run in constant time.\nSo If we use Stack, the only additional thing we would have to implement is the `getMin()` function.\n\nWe will use one additional stack called the `minStack` to keep track of the minimum element in the stack at any point of time, this will allow us to get the minimum element from the stack in constant time.\n\n# Implementation 1 : getMin() = O(n) (Note, getMin() doesn't run in constant time) 😉\n\n```java\nclass MinStack {\n    Stack\u003cInteger\u003e stack = new Stack\u003c\u003e();\n    \n    public void push(int x) {\n        stack.push(x);\n    }\n    \n    public void pop() {\n        stack.pop();\n    }\n    \n    public int top() {\n        return stack.peek();\n    }\n    \n    public int getMin() {\n        Stack\u003cInteger\u003e s = new Stack\u003c\u003e();\n        int min = Integer.MAX_VALUE;\n        while(!stack.isEmpty()) {\n            int x = stack.pop();\n            min = Math.min(min, x);\n            s.push(x);\n        }\n        while(!s.isEmpty()) {\n            stack.push(s.pop());\n        }\n        return min;\n    }\n}\n```\n\n# Implementation 2 : getMin() = O(1)\n\n```java\nclass MinStack {\n   private Stack\u003cInteger\u003e stack = new Stack\u003c\u003e();\n   private Stack\u003cInteger\u003e minStack = new Stack\u003c\u003e();\n  \n   public void push(int x) {\n        stack.push(x);\n        if (minStack.isEmpty() || x \u003c= minStack.peek()) {\n            minStack.push(x);\n        }\n    }\n     \n   public void pop() {\n        if (stack.peek().equals(minStack.peek())) {  // Don't use == , stack.peek() == minStack.peek()\n            minStack.pop();\n        }\n       stack.pop();\n   }\n   \n   public int top() {\n        return stack.peek();\n   }\n \n   public int getMin() {\n        return minStack.peek();\n   }\n}\n\n```\n\n# Gotcha : Use equals when working with Integer object (don't use == operator)\n```java\n\nclass Main {\n  public static void main(String[] args) {\n    Integer a = new Integer(19);\n    Integer b = new Integer(19);\n    System.out.println(a == b);  // false\n    System.out.println(a.equals(b));  // true\n  }  \n}\n```\n\n### Key Points :\n1. Don't forget to update the minStack on both push and pop operations on the main stack\n2. Note : We push an element to minStack, only if the element is less than **or equal to** `min_stack.peek()` \n\nAs a side note, in Java calling `push()`, `pop()` and `peek()` returns the element, but in this question for `push()` and `pop()` the return type is void. And also calling  `pop()` and `peek()` on an empty stack results in `EmptyStackException`\n\n# References :\n1. https://www.youtube.com/watch?v=nGwn8_-6e7w\n2. https://www.youtube.com/watch?v=WxCuL3jleUA\n3. https://leetcode.com/problems/min-stack/solution/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fmin-stack","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Fmin-stack","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fmin-stack/lists"}