Stack with find-min/find-max more efficient than On?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Stacks are a fundamental data structure used extensively in algorithms and application design. The standard stack operations include `push` and `pop`, both providing efficient constant-time execution . However, additional operations like finding the minimum or maximum value in the stack often incur higher computational costs, potentially up to , where is the number of elements in the stack. This article explores techniques to enhance these operations to be more efficient than .
Fundamentals of Stack Operations
A stack is characterized by its Last-In-First-Out (LIFO) nature. Here's a quick recap on standard stack operations: • Push: Add an element to the top of the stack. • Pop: Remove the element from the top of the stack. • Peek: View the top element without removing it.
Extending Stack: Efficient Find-Min/Find-Max Operations
The naive approach to finding the minimum or maximum in a stack requires examining all elements, yielding a time complexity of . However, by augmenting the stack with additional data structures, you can achieve constant-time for these operations.
Technique 1: Using an Auxiliary Stack
This approach involves maintaining an auxiliary stack alongside the primary stack. Each auxiliary stack helps track the running minimum or maximum.
• Find-Min using an Auxiliary Stack: • Push: When pushing an element onto the main stack, also push onto the auxiliary stack only if the auxiliary stack is empty or the current element is less than or equal to the top of the auxiliary stack. • Pop: When popping from the main stack, also pop from the auxiliary stack if the popped value is equal to the top of the auxiliary stack. • Get-Min: Simply return the top of the auxiliary stack.
• Find-Max: A similar process can be used, except in this case, you maintain the maximum on the auxiliary stack instead of the minimum.
• Push: Store a tuple with the item and the minimum (or maximum) seen so far. • Pop: Simply pop off the stacks. • Get-Min/Max: Return the min (or max) value stored in the top element of the tuple.
• The table illustrates that both the auxiliary stack and pair elements approaches offer complexity for Min/Max operations compared to for naive methods.
Related reading
- Stackless pre-order traversal in a binary tree
- StackOverflowError in Math.Random in a randomly recursive method
- Statistic estimation of total nodes in a tree where edge traversal is expensive
- stdaccumulate with a reference?
- STL for segment tree in C
- stl map performance?
- Start async operations, then await later
- static destructor

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.