27. Get Min from Stack

The problem can be found at the following link: Question Link

Problem Description

Implement a stack that supports the following operations in O(1) time:

  • push(x): Push an integer x onto the stack.

  • pop(): Remove the top element from the stack.

  • peek(): Return the top element of the stack. If empty, return -1.

  • getMin(): Retrieve the minimum element in the stack. If empty, return -1.

Examples

Example 1

Input:

q = 7
queries = [[1, 2], [1, 3], [3], [2], [4], [1, 1], [4]]

Output:

[3, 2, 1]

Explanation:

push(2) -> Stack: [2]
push(3) -> Stack: [2, 3]
peek() -> 3
pop() -> Stack: [2]
getMin() -> 2
push(1) -> Stack: [2, 1]
getMin() -> 1

Example 2

Input:

Output:

Explanation:

Constraints

  • $(1 \leq q \leq 10^5)$ (Number of queries)

  • $(1 \leq x \leq 10^9)$ (Stack elements are within this range)

My Approach

Using Two Stacks (O(1) Time, O(N) Space)

  1. Use two stacks:

    • s β†’ Normal stack for elements.

    • minStack β†’ Keeps track of minimum values.

  2. When pushing an element x, check:

    • If minStack is empty or x <= minStack.top(), push x into minStack.

  3. While popping, if the popped element is the current min, pop from minStack.

  4. getMin() always returns minStack.top().

Time and Auxiliary Space Complexity

Operation

Time Complexity

Space Complexity

push(x)

O(1)

O(N) (extra stack)

pop()

O(1)

O(N) (extra stack)

peek()

O(1)

O(1)

getMin()

O(1)

O(1)

Code (C++)

πŸ“Œ Alternative Approaches

2️⃣ Using Single Stack with Pair (O(1) Space Overhead)

Approach

  • Instead of maintaining two stacks, store (value, min_so_far) as a pair in one stack.

  • This ensures getMin() always retrieves the min value in O(1) time.

πŸ”Ή Reduces extra space needed for minStack!

3️⃣ Using Single Stack with Variable (O(1) Extra Space)

Approach

  • Store the minimum value separately instead of using an extra stack.

  • If x is less than the current minimum, push a modified value (2*x - min).

  • While popping, restore the previous minimum.

πŸ”Ή Uses O(1) extra space while maintaining O(1) operations!

Comparison of Approaches

Approach

⏱️ Time Complexity

πŸ—‚οΈ Space Complexity

βœ… Pros

⚠️ Cons

Two Stacks (s & minStack)

🟒 O(1)

🟑 O(N)

Simple & direct implementation

Extra stack memory required

Single Stack with Pair

🟒 O(1)

🟑 O(N)

Stores min directly in one stack

Uses pair<int, int> overhead

Single Stack with Variable

🟒 O(1)

🟒 O(1)

Space-efficient, no extra stack

Requires special encoding logic

πŸ’‘ Best Choice?

  • βœ… For space efficiency: Single stack with min encoding (O(1) space).

  • βœ… For clarity: Two stacks (s & minStack).

  • βœ… For best of both worlds: Single stack with pairs (O(1) operations).

Code (Java)

Code (Python)

Contribution and Support

For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Let’s make this learning journey more collaborative!

⭐ If you find this helpful, please give this repository a star! ⭐


πŸ“Visitor Count

Last updated