//辅助栈法
class MinStack {
Stack<Integer> a,b;
/** initialize your data structure here. */
public MinStack() {
a = new Stack<>();
b = new Stack<>();
}
public void push(int val) {
a.add(val);
if(b.isEmpty() || val <= b.peek())
b.add(val);
}
public void pop() {
if(a.pop().equals(b.peek()))
b.pop();
}
public int top() {
return a.peek();
}
public int getMin() {
return b.peek();
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(val);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
class MinStack {
Stack<Integer> A,B;
/** initialize your data structure here. */
public MinStack() {
A = new Stack<Integer>();
B = new Stack<Integer>();
}
public void push(int x) {
A.add(x);
if(B.empty() || B.peek() >= x){
B.add(x);
}
}
public void pop() {
if(A.pop().equals(B.peek())){
B.pop();
}
}
public int top() {
return A.peek();
}
public int getMin() {
return B.peek();
}
}
/**
解题思路:
雇佣一个辅助栈维护当前值和之前入栈的最小值,每次入栈,出栈,取最小值时间复杂度O(1), 空间复杂度O(N)
*/
class MinStack {
Deque<Integer> min;
Deque<Integer> st;
/** initialize your data structure here. */
public MinStack() {
min = new ArrayDeque<>();
st = new ArrayDeque<>();
}
public void push(int x) {
st.add(x);
if (min.isEmpty() || min.getLast() > x){
min.add(x);
} else{
min.add(min.getLast());
}
}
public void pop() {
st.pollLast();
min.pollLast();
}
public int top() {
return st.getLast();
}
public int getMin() {
return min.getLast();
}
}
private:
std::stack m_sk1;
std::stack m_sk2;
;
int m_nMinVal;
};
/**
* Your MinStack object will be instantiated and called as such:
* MinStack* obj = new MinStack();
* obj->push(x);
* obj->pop();
* int param_3 = obj->top();
* int param_4 = obj->getMin();
*/
辅助栈
class MinStack:
def __init__(self):
"""
initialize your data structure here.
"""
self.stack = []
# add a really large number for initial compare
self.min_stack = [math.inf]
def push(self, x: int) -> None:
# with the idea that the min will always be at top.
# then always add the existing min value
# into the actual min_stack.
self.stack.append(x)
self.min_stack.append(min(x, self.min_stack[-1]))
def pop(self) -> None:
self.stack.pop()
self.min_stack.pop()
def top(self) -> int:
return self.stack[-1]
def getMin(self) -> int:
return self.min_stack[-1]
class MinStack {
Stack<Integer> s;
Stack<Integer> min;
/** initialize your data structure here. */
public MinStack() {
s = new Stack<>();
min = new Stack<>();
min.push(Integer.MAX_VALUE);
}
public void push(int x) {
if (x <= min.peek()) {
min.push(x);
}
s.push(x);
}
public void pop() {
if(s.peek().equals(min.peek())){
min.pop();
}
s.pop();
}
public int top() {
return s.peek();
}
public int getMin() {
return min.peek();
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
class MinStack {
private Stack<Integer> stack;
private Stack<Integer> help; // 定义一个辅助栈
/** initialize your data structure here. */
public MinStack() {
stack = new Stack<>();
help = new Stack<>();
}
public void push(int x) {
stack.push(x);
if(help.isEmpty()){
help.push(x);
} else {
int min = x < help.peek() ? x : help.peek();
help.push(min);
}
}
public void pop() {
stack.pop();
help.pop();
}
public int top() {
return stack.peek();
}
public int getMin() {
return help.peek();
}
}
class MinStack {
Stack stack1;
Stack stack2;
int min=0;
/** initialize your data structure here. */
public MinStack() {
stack1=new Stack<>();
stack2=new Stack<>();
}
public void push(int x) {
stack1.push(x);
}
public void pop() {
stack1.pop();
}
public int top() {
return stack1.peek();
}
public int getMin() {
min=stack1.peek();
while(!stack1.empty()){
if(stack1.peek()
class MinStack {
Stack stack1;
Stack stack2;
/** initialize your data structure here. */
public MinStack() {
stack1=new Stack<>();
stack2=new Stack<>();
}
public void push(int x) {
stack1.push(x);
if(stack2.isEmpty()||x<=stack2.peek().intValue())
stack2.push(x);
}
public void pop() {
if(!stack1.isEmpty()){
if(stack1.peek().intValue()==stack2.peek().intValue()){
stack2.pop();
}
stack1.pop();
}
}
public int top() {
return stack1.peek().intValue();
}
public int getMin() {
return stack2.peek().intValue();
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
方法二:时间长空间小,直接遍历全栈,寻找最小值,用stack的迭代器 时间O(N) 空间O(1)
class MinStack {
Stack stack;
/** initialize your data structure here. */
public MinStack() {
stack=new Stack<>();
}
public void push(int x) {
stack.push(x);
}
public void pop() {
stack.pop();
}
public int top() {
return stack.peek().intValue();
}
public int getMin() {
int min=stack.peek().intValue();
Iterator value=stack.iterator();
while(value.hasNext()){
Integer current=(Integer)value.next();
if(current.intValue()
/** initialize your data structure here. */
public MinStack() {
s1 = new Stack();
s2 = new Stack();
}
public void push(int x) {
s1.push(x);
if(s2.isEmpty()){
s2.push(x);
}else{
s2.push(Math.min(x, s2.peek()));
}
}
public void pop() {
s1.pop();
s2.pop();
}
public int top() {
return s1.peek();
}
public int getMin() {
return s2.peek();
}
class MinStack {
Stack<Integer> A, B;
/** initialize your data structure here. */
public MinStack() {
A = new Stack<>();
B = new Stack<>();
}
public void push(int x) {
// 先压入栈A
A.push(x);
// 如果栈B为空,或者顶部元素小于x,则入栈B
if (B.isEmpty() || x <= B.peek()) { // peek是只输出栈顶元素,不删除; pop是输出栈顶元素并删除
B.push(x);
}
}
public void pop() {
// A先出栈
Integer y = A.pop();
// 若B的栈顶元素等于A的栈顶元素,则一起删除
if (y.equals(B.peek())) { // 这里不能用 ==
B.pop();
}
}
public int top() {
return A.peek();
}
public int getMin() {
return B.peek();
}
}
class MinStack {
Stack<Integer> data;
Stack<Integer> min;
/**
* initialize your data structure here.
*/
public MinStack() {
data = new Stack();
min = new Stack();
}
public void push(int x) {
data.push(x);
if (min.isEmpty() || x <= min.peek()) {
min.push(x);
}
}
public void pop() {
if (!data.isEmpty()) {
if (data.peek().equals(min.peek())) {
min.pop();
}
data.pop();
}
}
public int top() {
return data.peek();
}
public int getMin() {
return min.peek();
}
}
class MinStack {
// 两个栈
Stack<Long> stack;
Stack<Long> min;
/** initialize your data structure here. */
public MinStack() {
stack = new Stack<>();
min = new Stack<>();
}
public void push(long x) {
if (stack.isEmpty()) {
min.push(x);
stack.push(x);
return;
}
stack.push(x);
min.push(Math.min(x, min.peek()));
}
public void pop() {
stack.pop();
min.pop();
}
public long top() {
return stack.peek();
}
public long getMin() {
return min.peek();
}
}
一个最小值辅助栈即可
class MinStack {
Stack stk = new Stack<>(), min = new Stack<>();
/** initialize your data structure here. */
public MinStack() {
}
public void push(int x) {
if(stk.isEmpty()){
stk.push(x);
min.push(x);
return;
}
stk.push(x);
min.push(Math.min(min.peek(), x));
}
public void pop() {
stk.pop();
min.pop();
}
public int top() {
return stk.peek();
}
public int getMin() {
return min.peek();
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
共以下 29 个回答
class MinStack {
public:
/** initialize your data structure here. */
MinStack() {
}
private: m_sk1; m_sk2;
std::stack
std::stack
;
int m_nMinVal;
};
/**
* Your MinStack object will be instantiated and called as such:
* MinStack* obj = new MinStack();
* obj->push(x);
* obj->pop();
* int param_3 = obj->top();
* int param_4 = obj->getMin();
*/
辅助栈
“`cpp; s; min;
中文
class MinStack {
std::stack
std::stack
public:
/** initialize your data structure here. */
MinStack() {}
};
思路:用两个辅助stack 一个存储所有的值 另一个每次push时记录当前遇到的最小值
这样可以保证第二个stack里能每次都很快的取得最小值
time O(1) space O(n) worst case O(2n).
思路:创建两个stack,其中一个栈(valstack)存储原始元素,一个栈(minvalstack)用来存储元素后的最小元素
时间:o(1)
空间:o(n)
“`c++
class MinStack {
public:
MinStack() {
private: stack1, stack2;
stack
};
//打卡
“`
1.思路分析
核心思路
双栈设计:
– 一个栈(stNormal)用于存储所有插入的元素,记录栈的完整数据。
– 另一个栈(stMin)用于存储每一步的最小值,记录当前栈的最小元素。
辅助栈的作用:
– 每次入栈操作时,辅助栈更新记录当前栈的最小值。
– 每次出栈操作时,辅助栈同步出栈,保证 stMin 栈顶始终是当前栈中最小的元素。
2.代码分析
3.复杂度分析
思路:用一个栈专门存截止到当前入栈元素为止,降序元素的顺序。
时间O(1),空间O(N)
双栈
时间复杂度O(1)
空间复杂度O(n)
时间:O(1)
空间:O(n)
思路: 使用两个栈,一个栈正常记录元素,一个栈记录当前最小值: 多当前元素小于栈顶,则当前元素为最小值,否则栈顶元素做为最小值
在构建栈的时候,用辅助栈维护最小值
“`c++sk; help;
class MinStack {
public:
/** initialize your data structure here. */
stack
stack
MinStack() {
};
“`
没有学过算法,初步写了一个通过用例但是时间超时的答案
解决方法:
方法一,空间大时间短,一开始写完不知道错在哪里,后来发现没有拆箱integer,导致有错误 时间O(1)空间O(N)
方法二:时间长空间小,直接遍历全栈,寻找最小值,用stack的迭代器 时间O(N) 空间O(1)
//设置一个保存最小值变化的栈
class MinStack {
看完解题视频之后理清了思路,第二个栈用来依次存放相对当前栈顶更小的数值,每次pop操作的时候会检查一下是否把当前最小值pop出去了,和第一个栈的状态始终保持一致,所以可以在O(1)的时间复杂度来找到min值
class MinStack { s1; s2;
Stack
Stack
}
时间:O(1)
空间:O(n)
打卡,维护两个栈一个存放全部值一个存放最小值。
时间复杂度O(1)空间复杂度O(n)
时间复杂度:O(1)
额外空间复杂度:O(n)
思路:通过维护两个栈,一个存储所有元素,另一个存储当前的最小值,可以在常数时间内完成所有操作。
时间复杂度:所有的操作的时间复杂度均为 O(1)。
空间复杂度:O(n),需要额外的空间来存储最小值。
一个最小值辅助栈即可 stk = new Stack<>(), min = new Stack<>();
class MinStack {
Stack
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/