155. Min Stack

class MinStack:
    """
    Every time we push a value onto the stack, we also record the current minimum.
    """
    def __init__(self):
        # stack[i] = (val at i, minimum of stack up to and including i)
        self.stack = []
 
    def push(self, val: int) -> None:
        if not self.stack:
            self.stack.append((val, val))
        else:
            curmin = self.stack[-1][1]
            self.stack.append((val, min(curmin, val)))
 
    def pop(self) -> None:
        self.stack.pop()
        
    def top(self) -> int:
        return self.stack[-1][0]
 
    def getMin(self) -> int:
        return self.stack[-1][1]
  • once again, we use more data to save on time complexity.
  • every time we push into the stack, we also push in the minimum value of the stack up to and including the current value.

155-e1.excalidraw

⚠ Switch to EXCALIDRAW VIEW in the MORE OPTIONS menu of this document. ⚠

Text Elements

(-3, -3)

(-2, -2)

(0, -2)

Link to original

  • then, if we pop the value that is the current minimum, we know that the next minimum is the value stored in the top of the stack at the [1] index.

Categories:: stack, lc-system-design