[C++]LeetCode: 26 Min Stack

题目:

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

  • push(x) -- Push element x onto stack.
  • pop() -- Removes the element on top of the stack.
  • top() -- Get the top element.
  • getMin() -- Retrieve the minimum element in the stack.
思路:该栈支持栈的push, pop, top功能,需要增加一个常数时间内返回的getmin功能,需要维护一个最小栈。(普通遍历O(N) 不是常数时间)

Attention: 等于最小栈栈顶的元素也要push进最小栈,可能有多个相等的最小值。

复杂度:O(1)

AC Code:

class MinStack {
public:
    //维护一个最小栈,这样在常量时间返回栈的最小值。
    void push(int x) {
        s.push(x);
        //小于等于都要压栈
        if(mins.empty() || (x <= mins.top())){
            mins.push(x);
        }
    }

    void pop() {
        int pop = s.top();
        s.pop();
        if(pop == mins.top()){
            mins.pop();
        }
    }

    int top() {
        return s.top();
    }

    int getMin() {
        return mins.top();
    }

private:
    stack<int> s;
    stack<int> mins;
};


下面是一个巧妙的解法空间上只需要一个栈,栈中存储的是当前值x和当前min的插值,min维护当前最小值。

入栈操作:真正入栈的是值x和pre_min的差值,然后更新最小值min,等到最新的min值。

出栈操作:由于入栈操作可能需要更新最小值min,所以出栈操作会影响到剩余元素的最小值,需要求出pre_min。根据入栈时,min值是否发生改变,我们来判断出栈时min值是否需要更新。若入栈时,min未发生更新,即top()>=0时,直接删除栈顶元素。如果入栈时,min值发生改变,即top()<0时,此时有x-pre_min=top(),故可求pre_min=min(x)- top()。

取栈顶元素操作:由于栈顶存储差值,所以需要计算相应的值x。要么是min,要么是min+top()。判断条件是,在入栈时min是否改变,若改变,即top()<0,此时min等于x,返回min;如果没有改变,即top()>=0,返回min+top()。

Attention: 考虑可能产生正负数,使用long类型。

AC Code:

class MinStack
{
    static class Element
    {
        final int value;
        final int min;
        Element(final int value, final int min)
        {
            this.value = value;
            this.min = min;
        }
    }
    final Stack<Element> stack = new Stack<>();

    public void push(int x) {
        final int min = (stack.empty()) ? x : Math.min(stack.peek().min, x);
        stack.push(new Element(x, min));
    }

    public void pop()
    {
        stack.pop();
    }

    public int top()
    {
        return stack.peek().value;
    }

    public int getMin()
    {
        return stack.peek().min;
    }
}

Code: long long 类型超出leetcode 限制

class MinStack {
public:
    void push(int x) {
        if(s.empty()){
            //第一个值需要push 0(0的Long), 记得stack存储的是和当前上一个最小值的差值。push(x -x)
            s.push(0L);
            min = x;
        }
        else{
            s.push(x - min);
            if(x < min) min = x;
        }
    }

    void pop() {
        long long tmp = s.top();
        s.pop();
        if(tmp < 0) min = min - tmp;
    }

    int top() {
        long long tmp = s.top();
        if(tmp > 0){
            return (int)(min + tmp);
        }
        else{
            return (int)min;
        }
    }

    int getMin() {
        return (int)min;
    }
    
private:
    stack<long long> s;
    long long min;
};




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值