57 / 75
Use two stacks: one for values, one tracking minimum at each level.
Example:
Input:
push(-2), push(0), push(-3), getMin(), pop(), top(), getMin()Output:
[null, null, null, -3, null, 0, -2]Common Mistakes:
- Only storing single min value (fails when min is popped)
- Not handling duplicate minimums correctly
- Using == instead of equals() for Integer comparison
- Trying to find min by scanning stack (not O(1))
Notes:
All operations O(1) time. Space O(n) for two stacks. Alternative: store pairs (value, min) in single stack.
💻
Java Solution Hidden
Enable “Show Full Solution” to view the code
Use two stacks: one for values, one tracking minimum at each level.
Example:
Input:
push(-2), push(0), push(-3), getMin(), pop(), top(), getMin()Output:
[null, null, null, -3, null, 0, -2]Common Mistakes:
- Only storing single min value (fails when min is popped)
- Not handling duplicate minimums correctly
- Using == instead of equals() for Integer comparison
- Trying to find min by scanning stack (not O(1))
Notes:
All operations O(1) time. Space O(n) for two stacks. Alternative: store pairs (value, min) in single stack.
57/75
Min Stack