题目描述:

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.

解题思路:

这个问题挺简单(确实leetcode给的评级也是easy),但是还真是遇上了不少的问题。
首先,这道题目我们的想法是如何去做到这个特殊的getMin,想到的方法当然是空间换时间啦,那么用什么空间呢?当然是另一个栈啦,所以我们就有了这么一个想法:用另一个栈来记录当前最小值,那么查找最小值就不需要遍历了,这样就实现了时间复杂度和空间复杂度都是O(n)。的确这个想法已经很不错了,用java(官方题解就是这个版本)和C++(亲测)。但是我用python就MLE了,让我纠结了好久。
所以在没办法就要优化内存了,这里采用的方法是在minStack中插值的时候对相同的值不重复插入,而是记录他的次数,终于AC

MLE

 class MinStack:
def __init__(self):
self.stack = []
self.minStack = []
# @param x, an integer
# @return an integer
def push(self, x):
self.stack.append(x)
if len(self.minStack) == 0 or self.minStack[-1] >= x:
#print 'minn change'
self.minStack.append(x) # @return nothing
def pop(self):
p = self.stack.pop()
#print 'pop ' , p
if p == self.minStack[-1]:
#print 'minn pop'
self.minStack.pop() # @return an integer
def top(self):
return self.stack[-1] # @return an integer
def getMin(self):
return self.minStack[-1]

AC

 class MinStack:
def __init__(self):
self.stack = []
self.minStack = []
#self.minStack.append(0)
# @param x, an integer
# @return an integer
def push(self, x):
self.stack.append(x)
if len(self.minStack) == 0 or self.minStack[-1][0] > x:
#print 'minn change'
self.minStack.append((x,1))
elif x == self.minStack[-1][0]:
self.minStack[-1] = (x, self.minStack[-1][1] + 1) # @return nothing
def pop(self):
p = self.stack.pop()
#print 'pop ' , p
if p == self.minStack[-1][0]:
if self.minStack[-1][1] > 1:
#print 'minn pop'
self.minStack[-1] = (self.minStack[-1][0], self.minStack[-1][1] - 1)
else:
self.minStack.pop() # @return an integer
def top(self):
return self.stack[-1] # @return an integer
def getMin(self):
#print self.minStack
return self.minStack[-1][0]

【leetcode】Min Stack -- python版的更多相关文章

  1. [leetcode] Min Stack @ Python

    原题地址:https://oj.leetcode.com/problems/min-stack/ 解题思路:开辟两个栈,一个栈是普通的栈,一个栈用来维护最小值的队列. 代码: class MinSta ...

  2. leetCode Min Stack解决共享

    原标题:https://oj.leetcode.com/problems/min-stack/ Design a stack that supports push, pop, top, and ret ...

  3. LeetCode: Min Stack 解题报告

    Min Stack My Submissions Question Solution Design a stack that supports push, pop, top, and retrievi ...

  4. [LeetCode] Min Stack 最小栈

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

  5. [LeetCode] Min Stack

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

  6. LeetCode——Min Stack

    Description: Design a stack that supports push, pop, top, and retrieving the minimum element in cons ...

  7. LeetCode() Min Stack 不知道哪里不对,留待。

    class MinStack { public: MinStack() { coll.resize(2); } void push(int x) { if(index == coll.size()-1 ...

  8. [LeetCode] Min Stack 栈

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

  9. LeetCode Min Stack 最小值栈

    题意:实现栈的四个基本功能.要求:在get最小元素值时,复杂度O(1). 思路:链表直接实现.最快竟然还要61ms,醉了. class MinStack { public: MinStack(){ h ...

随机推荐

  1. jquery 离开页面提示信息

    <script> $(window).bind('beforeunload', function () { return '您输入的内容尚未保存,确定离开此页面吗?'; });</s ...

  2. Formal Definitions Using ASN.1 - SNMP Tutorial

    30.7 Formal Definitions Using ASN.1 The SMI standard specifies that all MIB variables must be define ...

  3. Angular.js内置的63个指令

  4. .net core

  5. Solve

    /// <summary> /// Solves this instance. /// </summary> /// <returns>IFeatureClass. ...

  6. Python学习笔记——字典

    1.创建字典和给字典赋值,可以使用工厂方法dict()来创建字典,也可以使用fromkeys()来创建一个元素具有相同值的字典 >>> dict = {'name':'XiaoMin ...

  7. ecshop 多表删除

    $sql = 'DELETE O, G FROM ' . $GLOBALS['ecs']->table('delivery_order') . ' AS O, ' . $GLOBALS['ecs ...

  8. LoadRunner 获取接口请求响应信息

    Action() { int nHttpRetCode; // 默认最大长度为256,get请求需注意缓存问题,需要根据content-length进行修改 web_set_max_html_para ...

  9. Apache Shiro 学习记录1

    最近几天在学习Apache Shiro......看了一些大神们的教程.....感觉收获不少.....但是毕竟教程也只是指引一下方向....即使是精品教程,仍然有很多东西都没有说明....所以自己也稍 ...

  10. 高效的jQuery

    选择捷径 // 糟糕 if(collection.length > 0){..} // 建议 if(collection.length){..} 熟记技巧 // 糟糕 $('#id').data ...