File tree 1 file changed +63
-0
lines changed
1 file changed +63
-0
lines changed Original file line number Diff line number Diff line change
1
+ class MinStack :
2
+ def __init__ (self ):
3
+ """ Creating stack minStack and size Variable"""
4
+ self .stack = []
5
+ self .minStack = []
6
+ self .size = 0
7
+
8
+ def push (self , data : int ) -> None :
9
+ if self .size == 0 :
10
+ self .minStack .append (data )
11
+ elif data <= self .minStack [- 1 ]:
12
+ self .minStack .append (data )
13
+ self .stack .append (data )
14
+ self .size = self .size + 1
15
+
16
+ def pop (self ) -> None :
17
+ """Removes the topmost element from the Stack """
18
+ top = self .stack .pop ()
19
+ self .size = self .size - 1
20
+ if top <= self .minStack [- 1 ]:
21
+ self .minStack .pop ()
22
+
23
+ def top (self ) -> None :
24
+ """Returns the top element from the stack """
25
+ return self .stack [- 1 ]
26
+
27
+ def getMin (self ):
28
+ """Returns the top element from the Minstack """
29
+ return self .minStack [- 1 ]
30
+
31
+
32
+ # Code execution starts here
33
+ if __name__ == "__main__" :
34
+
35
+ # Creating a min Stack
36
+ stack = MinStack ()
37
+
38
+ stack .push (6 )
39
+ print (stack .getMin ()) # prints 6
40
+
41
+ stack .push (7 )
42
+ print (stack .getMin ()) # prints 6
43
+
44
+ stack .push (8 )
45
+ print (stack .getMin ()) # prints 6
46
+
47
+ stack .push (5 )
48
+ print (stack .getMin ()) # prints 5
49
+
50
+ stack .push (3 )
51
+ print (stack .getMin ()) # prints 3
52
+
53
+ stack .pop ()
54
+ print (stack .getMin ()) # prints 5
55
+
56
+ stack .push (10 )
57
+ print (stack .getMin ()) # prints 5
58
+
59
+ stack .pop ()
60
+ print (stack .getMin ()) # prints 5
61
+
62
+ stack .pop ()
63
+ print (stack .getMin ()) # prints 6
You can’t perform that action at this time.
0 commit comments