Challenge: min( ) Function Using a Stack
Using your knowledge, create an efficient min() function using a stack.
We'll cover the following
Problem Statement
You have to implement the MinStack
class which will have a min()
function. Whenever min()
is called, the minimum value of the stack is returned in O(1) time. The element is not popped from the stack. Its value is simply returned.
Input
min()
operates on an object of MinStack
and doesn’t take any input
Output
Returns minimum number in O(1)
time
Sample Output
## min_stack = [9, 3, 1, 4, 2, 5]
## min_stack.min()
Result = 1
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.