Question: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. implement the MinStack class: - Minstack() initializes the stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. implement the MinStack class: - Minstack() initializes the stack object. - void push(int val) pushes the element yal onto the stack. - void pon( () removes the element on the top of the stack. - Int top( ) gets the top element of the stack. - int get Min() retrieves the minimum element in the stack. You must implement a solution with (1) time complexity for each function. Example 1: Input ["Minstack", "push", "push", "push", "getMin", "pop", "top", "getuin"] [[],[2],[],[3],[],[],[],[]] Output [ nu11, nu11, nu11, nu11, -3, nu11, 0,2] Explanation Minstack minstack - new Minstack(); minstack, push (2); minstack.push (0); ininstack.push (-3); minstack.getuin(); Il return -3 minstack,pop() minstack. top(). if return minstack.getmin( ) 11 return
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
