leetcode-study

155. Min Stack

class MinStack:
    """
    A stack data structure that supports push, pop, top, and retrieving 
    the minimum element in constant time.
    
    Attributes:
        stack (List[int]): The primary list storing all stack elements.
        min_stack (List[int]): A secondary list that keeps track of the 
                               current minimum element at each level of the stack.
    """
    def __init__(self):
        """
        Initialize the MinStack.
        """
        self.stack = []      # Primary stack to store the elements.
        self.min_stack = []  # Secondary stack to maintain current minimum for each push.

    def push(self, val: int) -> None:
        """
        Pushes the element 'val' onto the stack and updates the minimum stack.
        
        Parameters:
            val (int): The value to be pushed onto the stack.
        """
        # Add the element to the main stack.
        self.stack.append(val)
        # If min_stack is empty or the current value is smaller or equal to the last minimum, push it to min_stack.
        if not self.min_stack or val <= self.min_stack[-1]:
            self.min_stack.append(val)

    def pop(self) -> None:
        """
        Removes the element on the top of the stack and updates the minimum stack accordingly.
        """
        # Pop the top element from the main stack.
        popped = self.stack.pop()
        # If the popped element is the current minimum, also pop it from the min_stack.
        if popped == self.min_stack[-1]:
            self.min_stack.pop()

    def top(self) -> int:
        """
        Retrieves the top element of the stack without removing it.
        
        Returns:
            int: The top element of the stack.
        """
        return self.stack[-1]

    def getMin(self) -> int:
        """
        Retrieves the minimum element in the stack in constant time.
        
        Returns:
            int: The current minimum element in the stack.
        """
        return self.min_stack[-1]


# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(val)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin()

Summary of Techniques and Approaches: