Min Stack

Program for Implementing Min Stack Problem

Design a Min Stack class that efficiently supports the following operations, ensuring each runs in O(1) time complexity:

  • MinStack()
  • void push(int val)
  • void pop()
  • int top()
  • int getMin()

Implementation should maintain an auxiliary structure to keep track of the minimum values to ensure all operations run efficiently.

Implement Min Stack

Explanation:

MinStack minStack = new MinStack();
minStack.push(1);
minStack.push(2);
minStack.push(0);
minStack.getMin(); // return 0
minStack.pop();
minStack.top(); // return 2
minStack.getMin(); // return 1

Constraints:

  • -2^31 <= val <= 2^31 – 1.
  • pop, top and getMin will always be called on non-empty stacks.

Program for Implementing Min Stack Solution

Recommendation for Time and Space Complexity –You should aim for a solution with O(1) time for each function call and O(n) space, where n is the maximum number of elements present in the stack.

Hints for solving problems

Hint 1 :

A brute force solution would be to continuously remove valid brackets until no more can be removed. If the remaining string is empty, return true; otherwise, return false. This would result in an O(n^2) solution. Can we think of a better approach? Perhaps a data structure could help.A basic approach would involve checking the minimum element in the stack each time the getMin() function is called. However, this approach would take O(n) time. Can you think of a more efficient solution, possibly using O(n) extra space to store additional information?

Hint 2 :

We can use a stack to store the elements, but how can we keep track of the minimum element at any given point? Consider using a prefix strategy to keep track of the minimum value.

Hint 3 :

Use a second stack to store the minimum value at each step. When removing elements from the main stack, also remove the corresponding value from this auxiliary stack. While pushing elements, compare the current value with the top of the auxiliary stack and store the smaller value in the auxiliary stack. This ensures the auxiliary stack always holds the minimum value at the current state.

There are mainly 3 approach to solve this problem-

  1. Brute Force Method
  2. Two Stacks Method
  3. One Stack Method

1. Brute Force Method

Use a regular stack for storing elements and iterate through the stack whenever the minimum value is needed, resulting in O(n) time complexity for getMin().

  • Time complexity: O(n) for getMin() and O(1) for other operations.
  • Space complexity: O(n) for getMin() and O(1) for other operations.

Code

2. Two Stacks Method

Use one stack to store all elements and another stack to keep track of the current minimum values, ensuring O(1) time complexity for getMin().

  • Time complexity: O(1) for all operations.
  • Space complexity: O(n)

Code

3. One Stack Method

Store both the value and the minimum value at each step in a single stack using a tuple, maintaining O(1) time complexity for all operations.

  • Time complexity: O(1) for all operations.
  • Space complexity: O(n)

Code

More Articles

155. Min Stack Leetcode Solution

Min Stack Leetcode Problem :

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.

jump game leetcode

Min Stack Leetcode Solution :

Constraints :

  • -2^31 <= val <= 2^31 – 1
  • Methods pop, top and getMin operations will always be called on non-empty stacks.
  • At most 3 * 104 calls will be made to push, pop, top, and getMin.

Intuition :

The MinStack class is designed to maintain a stack of elements while also keeping track of the minimum element in the stack.

Approach :

1. The MinStack class uses a single stack, stk, to store pairs of elements and the minimum values encountered so far.
2: When an element is pushed onto the stack, the current minimum value is calculated by comparing the new element with the minimum value stored on top of the stack. The pair (element, current minimum) is then pushed onto the stack.
3: When an element is popped from the stack, it effectively removes the top pair, effectively reverting to the previous state.
4: The top function returns the first element of the top pair on the stack, and the getMin function returns the second element of the top pair, which represents the current minimum value.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Code :

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription