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