Minimum Stack
Implement a stack with the following methods:
MinimumStack()
constructs a new instance of a minimum stackappend(int val)
appendsval
to the stackpeek()
retrieves the last element in the stackmin()
retrieves the minimum value in the stackpop()
pops and returns the last element in the stack
Each method should be done in \(\mathcal{O}(1)\) time. You can assume that for peek
, min
and pop
, the stack is non-empty when they are called.
Constraints
n ≤ 100,000
wheren
is the number of calls toappend
,peek
,min
, andpop
.
https://binarysearch.com/problems/Minimum-Stack
Examples
Example 1
Input
- methods =
['constructor', 'append', 'append', 'min', 'peek', 'pop', 'pop']
- arguments =
[[], [1], [2], [], [], [], []]
Output
- answer =
[None, None, None, 1, 2, 2, 1]
Explanation
- We create a
Minimumstack
- We append
1
to the stack - We append
2
to the stack - We get the min value which is
1
- We get the top value which is
2
- We pop the top value which is
2
- We pop the top value which is
1
Leave a comment