forked from super30admin/Design-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path155_min_stack.py
132 lines (121 loc) · 3.37 KB
/
155_min_stack.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
from math import inf
class MinStack:
"""
Using single stack
Time Complexity: O(n)
Space Complexity: There are 2n elements in the stack but in Big O terms O(n)
"""
def __init__(self):
"""
initialize your data structure here.
"""
self.stack = []
self.min = inf
def push(self, x: int) -> None:
if x <= self.min:
self.stack.append(self.min)
self.min = x
self.stack.append(x)
def pop(self) -> None:
if self.stack.pop() == self.min:
self.min = self.stack.pop()
def top(self) -> int:
if self.stack:
return self.stack[-1]
def getMin(self) -> int:
return self.min
# from math import inf
#
# class MinStack:
# """
# Two stack solution
# """
#
# def __init__(self):
# """
# initialize your data structure here.
# """
# self.stack = []
# self.min_stack = [inf]
#
# def push(self, x: int) -> None:
# self.stack.append(x)
# self.min_stack.append(min(x, self.getMin()))
#
# def pop(self) -> None:
# if self.stack:
# self.stack.pop()
# self.min_stack.pop()
#
# def top(self) -> int:
# if self.stack:
# return self.stack[-1]
#
# def getMin(self) -> int:
# if self.min_stack:
# return self.min_stack[-1]
# class MinStack:
# """
# // Time Complexity :
# O(n)
# // Space Complexity :
# O(n)
# // Did this code successfully run on Leetcode :
# Yes
# // Any problem you faced while coding this :
# Took to time to dice to store the min in tuple.
# I was initially storing the min in a local variable and recalculating each time
# // Your code here along with comments explaining your approach
# We have a list of tuples. Where the first index is the value in the stacj
# and the second index stores the current min value until that value in the stack.
# When ever we insert a new value we compare with the last inserted values min value in
# the tuple and insert the lowest.
# """
#
# def __init__(self):
# """
# Time Complexity: O(1)
# Space Complexity: O(1)
# """
# self.stack = []
#
# def push(self, x: int) -> None:
# """
# n is the number of elements in the list
# Time Complexity: O(n)
# Space Complexity: O(1)
# """
# if self.stack:
# self.stack.append((x, min(x, self.getMin())))
# else:
# self.stack.append((x, x))
#
# def pop(self) -> None:
# """
# Time Complexity: O(1)
# Space Complexity: O(1)
# """
# if self.stack:
# self.stack.pop()
#
# def top(self) -> int:
# """
# Time Complexity: O(1)
# Space Complexity: O(1)
# """
# if self.stack:
# return self.stack[-1][0]
#
# def getMin(self) -> int:
# """
# Time Complexity: O(1)
# Space Complexity: O(1)
# """
# if self.stack:
# return self.stack[-1][1]
# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(x)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin()