Challenge: Create Stack Where min() Gives Minimum in O(1)

Can you create a stack where min() method gives minimum value in O(1) time? A solution is placed in the "solution" section to help you, but we would suggest you try to solve it on your own first.

Problem Statement #

In this problem, you have to implement a new kind of stack called MinStack, which can get the minimum value in O(1) time. An illustration is also provided for your understanding.

Note: MinStack only deals with integer type values.

Method Prototype: #

int min()

Output: #

It returns the minimum number in O(1) time

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.