Min Stack

Problem Description

Design a stack that supports push, pop, top, and retrieving the minimum element in O(1) time. Implement MinStack() and: - push(val): Push val onto the stack. - pop(): Remove the top element. - top(): Return the top element. - getMin(): Retrieve the minimum element. DE Shaw Tier 2 — LC 155. Optimal: Two stacks — main stack and minStack. On push, also push to minStack if val <= minStack.top(). On pop, also pop from minStack if top equals popped value. Alternative: Store (val, currentMin) pairs in one stack.

Example Test Cases

Example 1
Input: [["MinStack","push","push","push","getMin","pop","top","getMin"],[[],[-2],[0],[-3],[],[],[],[]]]
Expected: [null,null,null,null,-3,null,0,-2]
Example 2
Input: [["MinStack","push","push","getMin","push","getMin"],[[],[5],[3],[],[7],[]]]
Expected: [null,null,null,3,null,3]

Run your code to see the results