用stack实现min stack

遇到个好玩的问题,就是用一个stack实现min stack,什么意思呢,就是我实现stack,但是能以O(1)的时间复杂度和空间复杂度去找到我stack里面的最小值。

常规的方法是:用一个变量存放当前最小值,但是会出现这种情况,就是当我的stack pop掉的值刚好是最小值时候,后面就没法知道当前的最小值了。

怎么办呢?可以考虑在push阶段做个改变,就是在我每次往stack里面push数据的时候,跟当前的最小值比较,如果比当前最小值还小的话,那么将当前最小值入栈,再把最小值修改为这个值。在pop阶段,如果遇到pop出来的值刚好等于当前的最小值,那么将下一个值pop出来,并将pop出来的值设置为当前栈中最小值,这个值就是在push阶段时放进去的,表明这个位置的下面位置都比这个值小。

附录python源码:

class MinStack(object):

    def __init__(self):

        self.stack = []
        self.min = 0

    def push(self, m):

        if(len(self.stack) == 0):
            self.min_ = m
            self.stack.append(m)
        else:
            if m <= self.min_:
                self.stack.append(self.min_)
                self.min_ = m
                self.stack.append(m)
            else:
                self.stack.append(m)

    def pop(self):

        if self.stack[len(self.stack) - 1] == self.min_:
            self.stack.pop()
            self.min_ = self.stack.pop()
        else:
            self.stack.pop()

    def get_min(self):
        return self.min_

if __name__ == ‘__main__‘:
    min_stack = MinStack()
    min_stack.push(4)
    min_stack.push(5)
    min_stack.push(7)
    min_stack.push(6)
    min_stack.push(-3)
    min_stack.push(-6)
    min_stack.push(18)
    min_stack.pop()
    min_stack.pop()
    print min_stack.get_min()

  

时间: 2024-12-26 15:17:05

用stack实现min stack的相关文章

2016.6.1——Min Stack

Min Stack 本题收获: 1.可以利用两个栈操作. 2.栈的基本操作. 题目: 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

[leedcode 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 minimum e

【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 My Submissions Question Solution 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

【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 minimum e

[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

Solution 2: Min Stack

问题描述 定义栈的数据结构,要求添加一个min函数,能够得到栈的最小元素. 实现栈的push(), pop()及getMin()函数,要求函数的时间复杂度为O(1). 解决思路 使用两个栈,一个为普通栈,实现push和pop函数:另一个记录所有入栈元素的非递增序列: 如下图所示: 程序 public class MinStack { private Stack<Integer> normal; private Stack<Integer> min; public MinStack(

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

lintcode 中等题:Min stack 最小栈

题目 带最小值操作的栈 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. 解题 可以定义一个数组或者其他的存储最小值,第i个元素,表示栈中前i个元素的最小值. 定义两个ArrayList来存储栈,一个ArrayList存储当前栈中的元素,一个ArrayList存储最小栈,并且其第i个元素表示栈中前i个元素的最小值,这样两个栈的长度是始终一样的 入栈:最小栈需要加入的元素是 当前要入的元