Search⌘ K
AI Features

Solution Review: Min Stack

Explore how to create a Min Stack class in Go by managing two stacks for efficient push, pop, and retrieving minimum operations. Understand the logic that maintains the current minimum value at the top of a secondary stack, enabling constant time access.

Solution: A two-stack class

We’ll make a new class consisting of two stacks: mainStack and minimumStack. The class will have the following functions:

  • Push()
  • Pop()
  • Min()

Let’s see what each function will do:

  • Push: Push an element to the top of mainStack. It compares the new value with the value at the top of minimumStack. If the new value is smaller, then we push the new value into minimumStack. Otherwise, we ...