Difficulty: Medium, Asked-in: Adobe, Amazon, LinkedIn, Paytm, VMware.
Key takeaway: An excellent problem to learn data structure design.
Let’s understand the problem
Design and implement a stack that supports push, pop, top and retrieving the minimum element in constant time. In other words, our goal is to implement the MinStack class, which supports the following methods with O(1) time complexity.
- void push(int x): Insert element x onto the stack.
- void pop(): Remove the top element from the stack.
- int top(): Retrieve the top element.
- int getMin(): Retrieve the minimum element in the stack.
Note: The methods pop, top, and getMin will always be called on non-empty stacks.
Before solving this problem, if you want to learn about the design and implementation of stack operations, you can explore this blog: Introduction to the Stack Data Structure.
MinStack implementation using two stacks
Here we have one critical task: We need to track the current minimum element during push and pop operations so that we can access the minimum in O(1) time. So, how can we achieve this? Let's think!
If we observe, we need to know the history of previous minimum to track the current minimum. For example, if we push a new element x that is less than the current minimum, then x will become our new current minimum. Similarly, if we pop the current minimum element, we need to update the current minimum with the previous minimum which can be found below the stack (requiring iteration over the stack to find it).
One idea would be to use two stacks to implement the MinStack: One stack to store elements (mainStack) and another stack (auxStack) to store the minimum elements at each stage. Our goal is to update the minimum element in auxStack when pushing and popping elements from the mainStack.
Push operation: We push the value into the mainStack. Now, we check if auxStack is empty or if the new value is less than or equal to the top value of auxStack. If either condition is true, we push the new value onto auxStack. This ensures that auxStack always contains the minimum at the top.
Pop operation: We remove the top value from the mainStack and check if the popped value is equal to the top value of auxStack. If they are equal, it means we are removing the minimum from the mainStack. So, we also remove the top value from auxStack.
Get minimum operation: We simply return the top value of auxStack, which is the current minimum value.
Top operation: We simply return the top value of the mainStack.
Implementation pseudocode
class MinStack
mainStack = empty stack
auxStack = empty stack
push(value)
push value onto mainStack
if auxStack is empty or value <= top of auxStack
push value onto auxStack
pop()
topValue = top of mainStack
pop mainStack
if topValue == top of auxStack
pop auxStack
getMinimum()
return top of auxStack
top()
return top of mainStackTime and space complexity analysis
In this algorithm, each operation will take O(1) time. We use an extra stack to store the record of the minimum element at each stage. So we will need O(n) space in the worst case. The critical question is: What would be the best and worst-case scenario? Explore and think!
Using one stack and pairs to store the value and current minimum
Solution idea
During push and pop operation, we need to track the minimum. The critical question is: Can we solve this using a single stack? One idea would be to use a pair to store two elements together: The first element stores the actual value, and the second element stores the minimum encountered so far.
Push operation
During the first push operation, we push the value into the stack and store it as the minimum in the pair. This is because there are no previous elements to compare, so the minimum encountered so far is the value itself.
In subsequent push operations, we check if the minimum stored in the top pair of the stack (the second element in the pair) is less than the new value. If it is, it means the minimum encountered so far remains the same, so we push the new value along with the same minimum value as a pair. Otherwise, if the new value is the new minimum, we push the new value and the updated minimum as a pair. This ensures that we always have the minimum stored in the second element of the top pair.
Pop operation: We simply remove the top element from the stack.
Top operation: We return the first element of the pair at the top of the stack.
Get minimum operation: The second element of the pair at the top of the stack will store the current minimum of the stack. So we return this value.
Implementation pseudocode
class MinStack
stack s // Stores pairs: (value, minimum so far)
push(x)
if s is empty
currentMin = x
else
currentMin = min(top(s).minimum, x)
push (x, currentMin) onto s
pop()
pop s
top()
return top(s).value
getMin()
return top(s).minimumTime and space complexity analysis
In the above approach, each operation has a time complexity of O(1). We use a single stack but store an additional detail for each element to track the minimum element. So space complexity remains O(n).
Implementation using linked list implementation of the stack
This idea is similar to the above approach, but instead, we use a linked list implementation of the stack. In this approach, we maintain a separate min variable in each node to store the minimum value up to that node. We update the min value at each node accordingly when we push or pop elements.
Solution steps
Here each linked list node stores three information: the value of the element, the minimum value up to that node and the next pointer. We define a private head variable, which points to the topmost node in the stack.
- Push operation: If the stack is empty (head == NULL), we create a new node with x as both the value and the minimum. Otherwise, we create a new node with x as the value and the min (x, head->min) as the minimum. We update this newly created node as the new head.
- Pop operation: We remove the top element by updating the head to point to the next node.
- Top operation: We return the value of the top element in the stack (head->val).
- Get minimum operation: We return the value stored in the min variable of the top node (head->min).
Implementation pseudocode
class Node
val
min
next
Node(value, minimum, nextNode)
val = value
min = minimum
next = nextNode
class MinStack
head = null
push(x)
if head is null
head = new Node(x, x, null)
else
head = new Node(x, min(x, head.min), head)
pop()
temp = head
head = head.next
delete temp
top()
return head.val
getMin()
return head.minTime and space complexity analysis
The time complexity of each operation is O(1). We store an additional detail within each node to track the minimum element, so the space complexity remains O(n).
Another implementation using stack
This idea is similar to the two-stack solution. There is one difference: We use the same stack to store both elements and track the history of minimum elements.
Whenever a new minimum x arrives, we push the current minimum, update the minimum with x and push x. So we store every instance of the minimum and the previous minimum together. During the pop operation, when we remove the current minimum, the previous minimum can be retrieved easily by another pop operation.
Here is another perspective to understand the idea:
- When the current minimum value changes after pushing the new value x, we also push the old minimum value along with x. Otherwise, we just push x.
- If the pop operation results in a change of the current minimum, we pop twice and update the current minimum to the previous minimum value. Otherwise, we pop once.
Let's understand this with an example. Suppose we push 3, 2, 4 and 1 into the stack.
- Push 3: min = 3, Stack = [3].
- Push 2: 2 is the new minimum. So we push the current minimum 3, update min = 2, and push 2. Stack = [3, 3, 2].
- Push 4: min = 2, Stack = [3, 3, 2, 4].
- Push 1: 1 is the new minimum. So we push the current minimum 2, update min = 1, and push 1. Stack = [3, 3, 2, 4, 2, 1].
Now we pop the top three elements from the stack.
- 1st pop: Current minimum 1 is at the top. So we pop 1, set the current minimum with the stack top (which is the previous minimum value 2), and pop 2. Stack = [3, 3, 2, 4].
- 2nd pop: We simply pop 4. Stack = [3, 3, 2]
- 3rd pop: Current minimum 2 is at the top. So we pop 2, set the current minimum with the stack top (which is the previous minimum value 3), and pop 3. Stack = [3].
Solution steps
Push operation: First, we check if x is less than or equal to the current minimum. If it is, x becomes the new minimum. We push the current minimum onto the stack before updating min to x. This preserves the previous minimum value, which can be restored later when we pop the minimum element. After handling the minimum value, we push the new element x onto the stack as usual.
Pop operation: First, we remove the top element from the stack and check if the popped value is equal to the current minimum. If it is, we need to update the minimum value. So, we pop the top element of the stack again (previous minimum value) and update the min with this value.
Top operation: We simply return the top element of the stack.
getMin operation: We return the value stored in the min variable.
Implementation pseudocode
class MinStack
min = INT_MAX
stack s
push(x)
if x <= min
// Save the previous minimum before updating it
push min onto s
min = x
push x onto s
pop()
if top of s == min
// Remove the current minimum
// then restore the previous one
pop s
min = top of s
pop s
top()
return top of s
getMin()
return minTime and space complexity analysis
Here each operation will take O(1) time. We are using just a single stack, but the size of this stack is larger compared to the previous approach. The idea is simple: we also push minimum elements in the same stack. So, in the worst case, the extra space used for the stack will be O(n). Think!
Critical ideas to think!
- Can we consider solving this problem using a different approach?
- What would be the space complexity of the last approach in the best and worst cases?
- After performing n push operations followed by n pop operations, how many standard stack operations will be required in the last approach?
- Suppose we also need to track the current maximum in O(1). How can we modify the above approaches?
Suggested coding problems to explore
- Given a stack, sort its values in ascending order.
- Given a stack, reverse the order of its elements.
- Design a data structure that supports inserting, delete, search and get random in constant time.
- Design a special stack such that the maximum element can be found in O(1) time and O(1) extra space.
- Design a Data structure which supports insertion and the first non-repeating element in O(1) time. Operations that are supported by the data structure:
- Implement stack using queue
- Implement queue using stack
If you have any queries or feedback, please write us at contact@enjoyalgorithms.com. Enjoy learning, Enjoy algorithms!
