-
Notifications
You must be signed in to change notification settings - Fork 305
/
minStack.py
48 lines (37 loc) · 1.07 KB
/
minStack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Author: Yu Zhou
# ****************
# 155. Min Stack
# Time: O(1)
# Space: O(n) using extra stack
# Descrption:
# 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.
# ****************
# 思路:
# 创建一个额外的stack用来储存所有的最小值
# 返回Min的时候,就返回minstack的最后一个值就好了
# ****************
# Final Solution *
# ****************
class MinStack(object):
def __init__(self):
self.stack = []
self.minstack = []
def push(self, x):
self.stack.append(x)
if self.minstack:
x = min(self.minstack[-1], x)
self.minstack.append(x)
def pop(self):
self.minstack.pop()
self.stack.pop()
def top(self):
return self.stack[-1]
def getMin(self):
return self.minstack[-1]