A minimum stack data structure is a type of stack that has an additional special feature. In addition to the usual stack operations like push and pop, it also allows for finding the minimum element in constant time. This special feature makes it particularly useful in scenarios where finding the minimum element is a common operation.
How Does a Minimum Stack Work?
A minimum stack is similar to a regular stack, but with an extra component that keeps track of the current minimum element. This extra component is typically implemented using an auxiliary stack, often referred to as the “minimum stack”.
When a new element is pushed onto the minimum stack, it is compared with the current minimum element. If it is smaller or equal, it becomes the new minimum and is pushed onto both the main stack and the minimum stack. If it is larger, only the main stack is updated.
When an element is popped from the main stack, it is also removed from the minimum stack if it happens to be the current minimum. This ensures that the top of the minimum stack always corresponds to the current minimum element in constant time.
Benefits of Using a Minimum Stack
The special feature of a minimum stack provides several advantages:
- Efficiently Finding Minimum: The ability to find the minimum element in constant time (O(1)) allows for quick retrieval without iterating through all elements in the stack.
- Optimized Algorithm Design: Algorithms that require finding and updating a running minimum can benefit from using a minimum stack. It eliminates unnecessary iterations and improves overall efficiency.
- Simplifies Code: By encapsulating both regular stacking functionality and efficient retrieval of minimum values, a minimum stack simplifies code implementation and improves code readability.
Let’s consider an example scenario where a minimum stack can be useful. Suppose we have a set of integers and we want to efficiently find the minimum element after performing a series of operations such as push, pop, and peek.
Using a minimum stack, we can push all the elements onto the stack while simultaneously updating the minimum element in constant time. Then, when needed, we can quickly retrieve the current minimum without iterating through all the elements.
Here’s an example code snippet in Python:
class MinimumStack: def __init__(self): self.stack =  self.min_stack =  def push(self, value): self.stack.append(value) if not self.min_stack or value <= self.min_stack[-1]: self.min_stack.append(value) def pop(self): if not self.stack: return None value = self.pop() if value == self.pop() return value def get_minimum(self): if not self.min_stack: return None return self.min_stack[-1]
In this example, we maintain two stacks: one for regular stacking functionality (self.stack) and another for keeping track of the minimum values (self.min_stack). The push operation updates both stacks efficiently, while pop removes elements from both stacks when necessary. The get_minimum function retrieves the current minimum element in constant time.
A minimum stack data structure provides an efficient way to find the minimum element in constant time while maintaining regular stack functionality. It offers benefits such as optimized algorithm design, simplified coding, and improved efficiency. By incorporating a separate auxiliary stack to track the minimum values, a minimum stack is a valuable tool in scenarios where finding the minimum element is a frequent operation.