-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsolution.py
More file actions
33 lines (28 loc) · 801 Bytes
/
solution.py
File metadata and controls
33 lines (28 loc) · 801 Bytes
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
# -*- coding:utf-8 -*-
class MinStack:
def __init__(self):
self.stack = []
self.min_stack = []
# @param x, an integer
# @return an integer
def push(self, x):
self.stack.append(x)
if not self.min_stack or x <= self.min_stack[-1]:
self.min_stack.append(x)
# @return nothing
def pop(self):
if not self.stack:
return None
if self.min_stack and self.min_stack[-1] == self.stack[-1]:
self.min_stack.pop()
return self.stack.pop()
# @return an integer
def top(self):
if not self.stack:
return None
return self.stack[-1]
# @return an integer
def getMin(self):
if not self.stack:
return None
return self.min_stack[-1]