Min Stack - Complete Solution Guide
Min Stack is LeetCode problem 155, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. Implement the MinStack class: MinStack() initializes the stack object. void push(int val) pushes the element val onto the stack. void pop() removes the element on the top of the stack. int top() gets the top element of the stack. int getMin() retrieves the minimum element in the stack. You must implement a solution with O(1) time complexity for each function. Example 1: Input ["MinStack","push","pus
Detailed Explanation
The problem requires designing a stack data structure with the additional functionality of retrieving the minimum element in O(1) time. The stack needs to support the standard `push`, `pop`, and `top` operations, along with a `getMin` operation. The input will be a series of calls to these operations on the `MinStack` object. The output will be the return values of the `top` and `getMin` operations, and `null` for `push` and `pop`.
Solution Approach
The solution uses two stacks: one for the main stack and another for tracking the minimum elements. When an element is pushed onto the main stack, it's also compared to the top of the min_stack. If the new element is less than or equal to the current minimum (top of min_stack), it's also pushed onto the min_stack. When an element is popped from the main stack, it's compared to the top of the min_stack. If they are equal, the top element is also popped from the min_stack. The `top` operation returns the top element of the main stack, and the `getMin` operation returns the top element of the min_stack. This allows for constant-time retrieval of the minimum element.
Step-by-Step Algorithm
- Step 1: Initialize two stacks: `stack` to store all elements and `min_stack` to store minimum elements.
- Step 2: In the `push` operation, push the given value `val` onto the `stack`.
- Step 3: In the `push` operation, if `min_stack` is empty or `val` is less than or equal to the current minimum (top of `min_stack`), push `val` onto the `min_stack`.
- Step 4: In the `pop` operation, pop the top element from the `stack`.
- Step 5: In the `pop` operation, if the popped element from `stack` is equal to the top element of `min_stack`, pop the top element from `min_stack`.
- Step 6: In the `top` operation, return the top element of the `stack`.
- Step 7: In the `getMin` operation, return the top element of the `min_stack`.
Key Insights
- Insight 1: Maintaining a separate stack to keep track of the minimum elements at each point of time.
- Insight 2: When pushing an element, compare it with the current minimum. If the new element is less than or equal to the current minimum, push it onto the min_stack.
- Insight 3: During pop operation, check if the element being popped from the main stack is equal to the top of the min_stack. If it is, pop from the min_stack as well. The equals comparison is important for cases where multiple identical min values exist.
Complexity Analysis
Time Complexity: O(1)
Space Complexity: O(n)
Topics
This problem involves: Stack, Design.
Companies
Asked at: Cisco, Delhivery, IMC, Informatica, Intuit, LinkedIn, Lucid, Lyft, MakeMyTrip, Morgan Stanley, Nike, Nvidia, Odoo, Oracle, Palo Alto Networks, PayPal, Paytm, Salesforce, Snap, Snowflake, Tinkoff, Veeva Systems, Vimeo, Walmart Labs, Yandex, Zenefits.