Min Stack DSA Solution - Auxiliary Stack - Study Chapter | QuizMaker

Min Stack explained with brute force, optimized auxiliary stack approach, dry run, edge cases, complexity, and Python, C++, Java code.

Read
12m
Type
Chapter
Access
Free

Course

DSA Course: Interview Patterns and Problem Solving

Topic

Module 4: Stack & Queue

Learning Outcome

After this lesson, you should be able to design a stack that supports push, pop, top, and getMin in constant time.

Problem Statement

Design a stack that supports standard stack operations and can return the minimum element currently in the stack in O(1) time.

OperationResultMinimum
push(-2), push(0), push(-3)Stack has three values-3
pop()Removes -3-2

Brute Force Approach

Use a normal stack and scan all current values whenever getMin is called.

That makes getMin O(n), which breaks the requirement.

Optimized Approach

Store each pushed value together with the minimum value at that moment. Then the stack top always knows the current minimum.

Another valid version uses two stacks: one for values and one for minimums. The pair approach is compact and easy to dry-run.

Exact Pseudocode

stack = empty stack of (value, currentMin)
push(value):
  if stack is empty:
    currentMin = value
  else:
    currentMin = min(value, stack.top.currentMin)
  push (value, currentMin)
pop():
  pop stack
top():
  return stack.top.value
getMin():
  return stack.top.currentMin

Reference Code

class MinStack:
    def __init__(self):
        self.stack = []

    def push(self, val):
        current_min = val if not self.stack else min(val, self.stack[-1][1])
        self.stack.append((val, current_min))

    def pop(self):
        self.stack.pop()

    def top(self):
        return self.stack[-1][0]

    def getMin(self):
        return self.stack[-1][1]
class MinStack {
    stack<pair<int, int>> st;

public:
    void push(int val) {
        int currentMin = st.empty() ? val : min(val, st.top().second);
        st.push({val, currentMin});
    }

    void pop() {
        st.pop();
    }

    int top() {
        return st.top().first;
    }

    int getMin() {
        return st.top().second;
    }
};
class MinStack {
    private Deque<int[]> stack = new ArrayDeque<>();

    public void push(int val) {
        int currentMin = stack.isEmpty() ? val : Math.min(val, stack.peek()[1]);
        stack.push(new int[] {val, currentMin});
    }

    public void pop() {
        stack.pop();
    }

    public int top() {
        return stack.peek()[0];
    }

    public int getMin() {
        return stack.peek()[1];
    }
}

Sample Dry Run

OperationStored pairStack stategetMin
push(-2)(-2, -2)[(-2,-2)]-2
push(0)(0, -2)[(-2,-2),(0,-2)]-2
push(-3)(-3, -3)[...,(-3,-3)]-3
pop()Remove top[(-2,-2),(0,-2)]-2

Complexity

OperationTimeSpace
push, pop, top, getMinO(1)O(n) total stack storage

Edge Cases

Interview Checklist

FAQs

Why store the current minimum with every value?

Because after a pop, the previous stack top immediately tells the previous minimum.

Can two stacks be used instead?

Yes. One stack can store values and another can store minimums. The pair approach is equivalent.

What is the core pattern?

Auxiliary minimum state.

Tags

Open on QuizMaker