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 elementvalonto the stack.void pop()removes the element on the top of the stack.int top()gets the top element of the stack.int getMin()retrieves the minimum element in the stack.
You must implement a solution with O(1) time complexity for each function.
Example 1:
Input ["MinStack","push","push","push","getMin","pop","top","getMin"] [[],[-2],[0],[-3],[],[],[],[]] Output [null,null,null,null,-3,null,0,-2] Explanation MinStack minStack = new MinStack(); minStack.push(-2); minStack.push(0); minStack.push(-3); minStack.getMin(); // return -3 minStack.pop(); minStack.top(); // return 0 minStack.getMin(); // return -2
Constraints:
-231 <= val <= 231 - 1- Methods
pop,topandgetMinoperations will always be called on non-empty stacks. - At most
3 * 104calls will be made topush,pop,top, andgetMin.
设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈。
实现 MinStack 类:
MinStack()初始化堆栈对象。void push(int val)将元素val推入堆栈。void pop()删除堆栈顶部的元素。int top()获取堆栈顶部的元素。int getMin()获取堆栈中的最小元素。
示例 1:
输入: ["MinStack","push","push","push","getMin","pop","top","getMin"] [[],[-2],[0],[-3],[],[],[],[]] 输出: [null,null,null,null,-3,null,0,-2] 解释: MinStack minStack = new MinStack(); minStack.push(-2); minStack.push(0); minStack.push(-3); minStack.getMin(); --> 返回 -3. minStack.pop(); minStack.top(); --> 返回 0. minStack.getMin(); --> 返回 -2.
提示:
-231 <= val <= 231 - 1pop、top和getMin操作总是在 非空栈 上调用push,pop,top, andgetMin最多被调用3 * 104次
| Language | Runtime | Memory | Submission Time |
|---|---|---|---|
| typescript | 112 ms | 51.3 MB | 2023/03/04 22:03 |
type StackItem = {
value: number;
minVal: number;
}
class MinStack {
private _stack: StackItem[];
private _min: number;
constructor() {
this._stack = [];
this._min = +Infinity;
}
push(val: number): void {
if (this._min > val) {
this._min = val;
}
this._stack.push({
value: val,
minVal: this._min
});
}
pop(): void {
this._stack.pop();
if (this._stack.length !== 0) {
this._min = this._stack[this._stack.length - 1].minVal;
} else {
this._min = +Infinity;
}
}
top(): number {
return this._stack[this._stack.length - 1].value;
}
getMin(): number {
return this._stack[this._stack.length - 1].minVal;
}
}
/**
* Your MinStack object will be instantiated and called as such:
* var obj = new MinStack()
* obj.push(val)
* obj.pop()
* var param_3 = obj.top()
* var param_4 = obj.getMin()
*/stackItem 保存一个入栈时的最小值状态即可。
type StackItem = {
value: number;
minVal: number;
}
class MinStack {
private _stack: StackItem[];
private _min: number;
constructor() {
this._stack = [];
this._min = +Infinity;
}
push(val: number): void {
if (this._min > val) {
this._min = val;
}
this._stack.push({
value: val,
minVal: this._min
});
}
pop(): void {
this._stack.pop();
if (this._stack.length !== 0) {
this._min = this._stack[this._stack.length - 1].minVal;
} else {
this._min = +Infinity;
}
}
top(): number {
return this._stack[this._stack.length - 1].value;
}
getMin(): number {
return this._stack[this._stack.length - 1].minVal;
}
}
/**
* Your MinStack object will be instantiated and called as such:
* var obj = new MinStack()
* obj.push(val)
* obj.pop()
* var param_3 = obj.top()
* var param_4 = obj.getMin()
*/注意: 栈清空时,需要重新把全局最小值设为+Infinity。