Search⌘ K
AI Features

Solution: Min Stack

Explore how to implement a stack that supports push, pop, and retrieving the minimum value all in constant time. Understand the use of two stacks—one for values and one for tracking minimums—to achieve optimal performance and space efficiency. This lesson guides you through the design, code, and complexity analysis of the min stack solution.

We'll cover the following...

Statement

Design a stack data structure to retrieve the minimum value in O(1)O(1) time. The following functions must be implemented:

  • min(): Returns the minimum value in the stack in constant time.
  • push(int value): Pushes a value onto the stack.
  • pop(): Removes and returns a value from the top of the stack.

All functions should be implemented with a time complexity of O(1)O(1).

Constraints:

  • 105-10^5 \leq
...