This topic aims to share some interesting or valuable topics found in the process of brushing Leecode. [Of course, the answer is based on js].
topic related
- Original title address
Topic description:
To define the data structure of the stack, please implement a min function in this type that can get the smallest element of the stack. In this stack, the time complexity of calling min, push and pop are all O(1).
MinStack minStack = new MinStack(); minStack.push(-2); minStack.push(0); minStack.push(-3); minStack.min(); --> 返回 -3. minStack.pop(); minStack.top(); --> 返回 0. minStack.min(); --> 返回 -2. // 解题框架如下 var MinStack = function() {}; MinStack.prototype.push = function(x) {}; MinStack.prototype.pop = function() {}; MinStack.prototype.top = function() {}; MinStack.prototype.min = function() {}; // 实际调用示例 // var obj = new MinStack() // obj.push(x)
Analysis of ideas
- First of all, let’s take a rough look at the
push
andpop
methods, which are relatively simple to implement, and can be implemented with the native methods of js arrays; - Secondly, the
top
method only needs to return the top element stack, that is, the last element array, which is relatively simple; - Then,
min
methods need to acquire a minimum, and requires time complexity is O (1) , which means wemin
approach, can not go through the array , must find ways to save in advance; and inpush
andpop
procedures, need to update the saved minimum value.
So here we know that the core difficulty of this topic lies in -- how to save the minimum value of the current data stack
Note here that, the question is designed to set the required stack structure, means that the data is only one way in and out of the way of , this is a critical prerequisite.
Next, follow the example steps of the title, and look at it step by step (look carefully or you will pass it with a swipe!):
Initially, the data stack is empty, first -2 is pushed into the stack, at this time:
数据栈[-2] // 此时 栈内最小元素值为-2
Then, when 0 is pushed into the stack, at this time:
数据栈[-2, 0] // 0 大于已有的最小值-2,那么最小值还是-2,
There is a key point here. In the current state, the way of elements from the stack, 161eaa04e3b2e9 can only be 0 first, then -2 , that is, before -2 pops the stack, the minimum value of the elements in the stack is always -2 . When you hear this, does it suddenly occur in your mind? ! ! ! .
Don't worry, continue to push -3, at this time:
栈内元素为【-2,0,-3】// 最小值应当更新为-3,
Similarly, when -3 is not popped from the stack, the minimum value in the stack is -3; when -3 is popped from the stack, the minimum value in the stack should be the previous minimum value -2;
Then the solution will emerge! ! !
We only need maintain a monotonically decreasing stack, and always only store elements smaller than the minimum value in the current stack!
(I know that some students still don't understand it when they see it, it doesn't matter) So let's still give an example:
// 初始状态如下
数据栈 [-2];
最小值栈 [-2];
// -2入栈后
数据栈 [-2];
最小值栈 [-2];
// 0入栈后 由于0比-2大,因此最小值栈不保存-2
数据栈 [-2, 0];
最小值栈 [-2];
// -3入栈后,-3比-2小,因此最小值栈保存-3
数据栈 [-2, 0, -3];
最小值栈 [-2, -3];
// -3出栈时,比较出栈元素是否是【最小值栈】的栈顶元素,是的话一起出栈;
数据栈 [-2, 0];
最小值栈 [-2];
It can be seen from this that the minimum value stack has always maintained a monotonically decreasing array, and the top element of the stack (the element at the end of the array) always represents the minimum value of the current data stack.
full code
After understanding the previous core content, in fact, the code is not difficult to write, and finally paste the completed code:
var MinStack = function() {
this.dataStack = [];
this.minStack = [];
};
MinStack.prototype.push = function(x) {
this.dataStack.push(x);
const len = this.minStack.length;
if(len === 0 || x <= this.minStack[len-1]){
this.minStack.push(x);
}
};
MinStack.prototype.pop = function() {
const last = this.dataStack.pop();
const len = this.minStack.length;
if(last === this.minStack[len-1]){
this.minStack.pop();
}
};
MinStack.prototype.top = function() {
const len = this.dataStack.length;
return len > 0 ? this.dataStack[len - 1] : null;
};
MinStack.prototype.min = function() {
const len = this.minStack.length;
return this.minStack[len-1];
};
A simple question is done again!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。