LeetCode[stack]: 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.

Discuss上的这个解法:https://oj.leetcode.com/discuss/15679/share-my-java-solution-with-only-one-stack 非常巧妙,算法的关键在于用一个stack保存了当前元素与最小元素的差。为了避免Integer.MAXVALUE-Integer.MINVALUE这个异常出现,采用Long类型来保存。Java代码如下:

public class MinStack {
    long min;
    Stack<Long> stack;

    public MinStack(){
        stack=new Stack<>();
    }

    public void push(int x) {
        if (stack.isEmpty()){
            stack.push(0L);
            min=x;
        }else{
            stack.push(x-min);//Could be negative if min value needs to change
            if (x<min) min=x;
        }
    }

    public void pop() {
        if (stack.isEmpty()) return;

        long pop=stack.pop();

        if (pop<0)  min=min-pop;//If negative, increase the min value

    }

    public int top() {
        long top=stack.peek();
        if (top>0){
            return (int)(top+min);
        }else{
           return (int)(min);
        }
    }

    public int getMin() {
        return (int)min;
    }
}

这个算法的时间性能如下:

但是我在用C++实现相同的算法时却得到“Memory Limit Exceeded”,这是因为当用long类型时,其实无异于用两个stack。尽管如此,这个算法的思想还是值得学习的。

时间: 2024-10-10 14:01:33

LeetCode[stack]: Min Stack的相关文章

【LeetCode】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 mini

[LeetCode] Min Stack Min Stack

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

Java [Leetcode 155]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 min

LeetCode 155 Min Stack(最小栈)

翻译 设计支持push.pop.top和在常量时间内检索最小元素的栈. push(x) -- 推送元素X进栈 pop() -- 移除栈顶元素 top() -- 得到栈顶元素 getMin() -- 检索栈的最小元素 原文 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop

[leetcode 155]min stack

1 题目 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 mini

Leetcode 155. Min Stack JAVA语言

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 e

Java for LeetCode 155 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

【leetcode】Min Stack -- python版

题目描述: 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

LeetCode之Min Stack

1.原文问题描述: 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