검 지 Offer 30 min 함 수 를 포함 하 는 스 택
스 택 의 데이터 구 조 를 정의 합 니 다. 이 형식 에서 스 택 에 포 함 된 최소 요 소 를 얻 을 수 있 는 min 함수 (시간 복잡 도 는 O (1) 를 실현 하 십시오.
1 # -*- coding:utf-8 -*-
2 class Solution:
3 def __init__(self):
4 self.stack = []
5 self.list = []
6
7 def push(self, node):
8 self.stack.append(node)
9 self.list.append(node)
10 self.list = sorted(self.list)
11 # write code here
12 def pop(self):
13 r = self.stack.pop(-1)
14 self.list.remove(r)
15 return r
16 # write code here
17 def top(self):
18 r = self.stack[-1]
19 return r
20 # write code here
21 def min(self):
22 r = self.list[0]
23 return r
24 # write code here