11"""
22Description:
3- Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
3+ Design a stack that supports push, pop, top, and retrieving the minimum element
4+ in constant time.
45
56Operations:
6- 1. push(x) -> Push element x onto stack.
7- 2. pop() -> Removes the element on top of the stack.
7+ 1. push(x) -> Push element x onto the stack.
8+ 2. pop() -> Removes the top element from the stack.
893. top() -> Get the top element.
9- 4. getMin() -> Retrieve the minimum element in the stack.
10+ 4. get_min() -> Retrieve the minimum element in the stack.
1011
1112Example:
1213 min_stack = MinStack()
1314 min_stack.push(-2)
1415 min_stack.push(0)
1516 min_stack.push(-3)
16- min_stack.getMin() # returns -3
17+ print( min_stack.get_min()) # Output: -3
1718 min_stack.pop()
18- min_stack.top() # returns 0
19- min_stack.getMin() # returns -2
19+ print( min_stack.top()) # Output: 0
20+ print( min_stack.get_min()) # Output: -2
2021
2122Time Complexity:
2223- push: O(1)
2324- pop: O(1)
2425- top: O(1)
25- - getMin : O(1)
26+ - get_min : O(1)
2627
2728Space Complexity:
28- - O(n) extra space for the min_stack
29+ - O(n) extra space for the auxiliary stack
2930"""
3031
3132
@@ -36,56 +37,38 @@ def __init__(self):
3637 - main_stack: stores all elements
3738 - min_stack: stores the current minimum element at each level
3839 """
39- self .main_stack = []
40- self .min_stack = []
40+ self .main_stack : list [ int ] = []
41+ self .min_stack : list [ int ] = []
4142
4243 def push (self , x : int ) -> None :
43- """
44- Push element x onto stack.
45-
46- Args:
47- x (int): Element to push
48- """
44+ """Push element x onto stack."""
4945 self .main_stack .append (x )
50- # Push to min_stack if it's empty or x is <= current minimum
5146 if not self .min_stack or x <= self .min_stack [- 1 ]:
5247 self .min_stack .append (x )
5348
5449 def pop (self ) -> None :
55- """
56- Removes the element on top of the stack.
57- """
50+ """Remove the element on top of the stack."""
5851 if self .main_stack :
5952 val = self .main_stack .pop ()
6053 if val == self .min_stack [- 1 ]:
6154 self .min_stack .pop ()
6255
63- def top (self ) -> int :
64- """
65- Get the top element of the stack.
66-
67- Returns:
68- int: Top element if stack is not empty, else None
69- """
56+ def top (self ) -> int | None :
57+ """Get the top element of the stack."""
7058 return self .main_stack [- 1 ] if self .main_stack else None
7159
72- def getMin (self ) -> int :
73- """
74- Retrieve the minimum element in the stack.
75-
76- Returns:
77- int: Minimum element if stack is not empty, else None
78- """
60+ def get_min (self ) -> int | None :
61+ """Retrieve the minimum element in the stack."""
7962 return self .min_stack [- 1 ] if self .min_stack else None
8063
8164
82- # Example Usage
65+ # Example usage
8366if __name__ == "__main__" :
8467 min_stack = MinStack ()
8568 min_stack .push (- 2 )
8669 min_stack .push (0 )
8770 min_stack .push (- 3 )
88- print ("Current Min:" , min_stack .getMin ()) # Output: -3
71+ print ("Current Min:" , min_stack .get_min ()) # Output: -3
8972 min_stack .pop ()
90- print ("Top Element:" , min_stack .top ()) # Output: 0
91- print ("Current Min:" , min_stack .getMin ()) # Output: -2
73+ print ("Top Element:" , min_stack .top ()) # Output: 0
74+ print ("Current Min:" , min_stack .get_min ()) # Output: -2
0 commit comments