Solution: Min Stack
Explore the implementation of a Min Stack, a custom data structure that supports push, pop, and retrieving the minimum element all in constant time. Understand how to use two stacks to maintain minimum values efficiently, ensuring all operations run in O(1) time. This lesson provides a practical approach to solving problems where tracking the minimum in a stack is required, strengthening your ability to create specialized data structures for coding interviews.
We'll cover the following...
Statement
Design a custom stack class, Min Stack, allowing us to push, pop, and retrieve the minimum value in constant time. Implement the following methods for Min Stack:
-
Constructor: This initializes the Min Stack object.
-
Pop(): This removes and returns from the stack the value that was most recently pushed onto it.
-
Push(): This pushes the provided value onto the stack.
-
Min Number(): This returns the minimum value in the stack in ...