zoukankan      html  css  js  c++  java
  • 【Python】LeetCode-155

    一、题目

    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.

    Example:

    MinStack minStack = new MinStack();
    minStack.push(-2);
    minStack.push(0);
    minStack.push(-3);
    minStack.getMin();   --> Returns -3.
    minStack.pop();
    minStack.top();      --> Returns 0.
    minStack.getMin();   --> Returns -2.
    

    二、思路&心得

      题意大致就是模拟栈的实现,这题比较出戏的一点就是要求在线性时间内获得栈中的最小值。一共有两个方法,都是比较巧妙的。

    方法一:

    • 维护两个自己构造的”栈“对象, 在这里为两个数组stack和sm,其中sm的栈顶为当前stack中最小元素;
    • 在push时要判断sm是否为空,如果为空或者非空但是栈顶元素大于等于插入值的 需要在sm中插入x;
    • 在pop时,s的元素被删除了,那么sm中的也应该被删除;
    • 通过这些操作维护sm能很巧妙在O(1)复杂度得到最小值。

    方法二:

    • 栈中的每个对象为(x, y),其中x为元素值,y为当前对象处于栈顶时,栈中的最小元素;
    • 在push的时候,令y为要push的值x和当前栈中最小值两个中的最小值;
    • 在top和getMin方法中,直接根据需要返回栈顶的第一个或第二个元素即可。

    三、代码

    方法一:

    class MinStack(object):
        def __init__(self):
            """
            initialize your data structure here.
            """
            self.stack = []
            self.sm = []
    
    
        def push(self, x):
            """
            :type x: int
            :rtype: void
            """
            self.stack.append(x)
            if not self.sm or self.sm[-1] >= x:
                self.sm.append(x)
    
        def pop(self):
            """
            :rtype: void
            """
            if self.sm[-1] == self.stack[-1]:
                self.sm.pop()
            self.stack.pop()
    
    
        def top(self):
            """
            :rtype: int
            """
            return self.stack[-1]
    
        def getMin(self):
            """
            :rtype: int
            """
            return self.sm[-1]
    

    方法二:

    class MinStack(object):
    
        def __init__(self):
            """
            initialize your data structure here.
            """
            self.stack = []
    
        def push(self, x):
            """
            :type x: int
            :rtype: void
            """
            if not (self.stack): 
                self.stack.append((x,x))
            else:
                self.stack.append((x, min(x, self.stack[-1][1])))
            
    
        def pop(self):
            """
            :rtype: void
            """
            if self.stack:
                self.stack.pop()    
            else:
                return None
            
    
        def top(self):
            """
            :rtype: int
            """
            if self.stack:
                return self.stack[-1][0]
            return None
            
        def getMin(self):
            """
            :rtype: int
            """
            if self.stack:
                return self.stack[-1][1]
            return None
    
  • 相关阅读:
    解决页面报错: GEThttp://localhost:8080/favicon.ico 404 (Not Found)
    vs2019快捷键整理
    js保存图片至本地
    PHP输出方式的区别
    js运算精度问题
    Hadoop相关问题
    2015年10月5日 12:49:07
    Hive数据倾斜
    Hadoop错误日志
    使用maven开发MR
  • 原文地址:https://www.cnblogs.com/CSLaker/p/7517542.html
Copyright © 2011-2022 走看看