Min Stack
July 27, 2012
If you ignore the requirement that all three operations must perform in constant time, this is easy. One choice creates a stack in the normal way, with push and pop operating in constant time, then performs linear search when the minimum is requested. Another choice uses some kind of balanced tree in which each operation is performed in logarithmic time. But getting a constant-time set of operations is hard.
Our solution achieves constant time for all three operations at the expense of auxiliary space equal to the size of the stack. The idea is to keep two stacks, one for the stack itself and the other for the stack of minimums. The push operation puts the new item at the top of the regular stack and, if it is less than the item at the top of the minimums stack, put it on top of the minimums stack as well. The pop operation removes the item at the top of the regular stack; it also removes the item at the top of the minimums stack if it is the same as the item popped from the top of the regular stack. To find the minimum, simply inspect the top of the minimums stack.
We represent a min-stack as a cons pair with the regular stack in its car and the min-stack in its cdr; for instance, the min-stack that results from inserting 3, 4, 5, 1 and 2 in order is ((2 1 5 4 3) 1 3), where the entire input is stacked (in reverse order) in the car of the list and the two minimums, 3 and 1, are stacked (in reverse order) in the cdr of the list. Here is the empty min-stack:
(define empty (list (list)))
(define (empty? x) (equal? empty x))
The push operation first checks if the min-stack is empty, and returns a pair of two singleton lists if it is. Otherwise, the new item is stacked onto the regular stack, and stacked onto the minimum stack only if it is smaller than the current minimum:
(define (push lt? x min-stack)
(if (empty? min-stack)
(cons (list x) (list x))
(cons (cons x (car min-stack))
(if (lt? x (cadr min-stack))
(cons x (cdr min-stack))
(cdr min-stack)))))
Since we represent a min-stack as a pair of lists, we get a good opportunity for a workout on the compositions of car (the head of the list) and cdr (the rest of the list) provided by Scheme. The top of the regular stack is the car of the inner list stored in the car of the pair, the caar, which is 2 in the sample shown above. The top of the minimum stack is the car of the cdr of the pair, the cadr, which is 1 in the sample shown above (the second 1, not the 1 in the inner parentheses). Here are two access functions:
(define (top min-stack)
(if (empty? min-stack)
(error 'top "empty stack")
(caar min-stack)))
(define (minimum min-stack)
(if (empty? min-stack)
(error 'minimum "empty stack")
(cadr min-stack)))
All that’s left is the pop operation, which needs to check if the item being popped is the same as the current minimum, in which case the current minimum is also popped; since we only have a lt? comparison, we call it twice to check equality:
(define (pop lt? min-stack)
(if (empty? min-stack)
(error 'pop "empty stack")
(cons (cdar min-stack)
(if (or (lt? (caar min-stack) (cadr min-stack))
(lt? (cadr min-stack) (caar min-stack)))
(cdr min-stack)
(cddr min-stack)))))
Here are some examples:
> (define x empty)
> (set! x (push < 3 x))
> (set! x (push < 4 x))
> (set! x (push < 5 x))
> (top x)
5
> (minimum x)
3
> x
((5 4 3) 3)
> (set! x (push < 1 x))
> (set! x (push < 2 x))
> (top x)
2
> (minimum x)
1
> x
((2 1 5 4 3) 1 3)
> (set! x (pop < x))
> (set! x (pop < x))
> x
((5 4 3) 3)
> (set! x (pop < x))
> (top x)
4
> (minimum x)
3
> (set! x (pop < x))
> (set! x (pop < x))
> (empty? x)
#t
> x
(())
You can run the program at http://programmingpraxis.codepad.org/EdKcpMok.
[…] today’s Programming Praxis exercise, our goal is to create s stack-like data structure that can push, pop […]
My Haskell solution (see http://bonsaicode.wordpress.com/2012/07/27/programming-praxis-min-stack/ for a version with comments):
Javascript. I used a function constructor.
Python version:
For the push operation, I think you need to add the new item to the min stack if it is returns 0, but also clears a 0 from the minimum stack
minimum –> should still be zero
Sorry, a less than symbol messed up my comment.
I think the push operation needs to add the new item to the minimum stack when it is less than or equal to the current minimum. Otherwise, a minimum item could get popped off to soon. Consider this sequence of operations:
The problem text states that all items are distinct.
Mike, pushing 3 onto the stack twice is actually not legal according the specification: “You may assume that all elements are distinct”.
Clojure solution almost similar to the Haskell one
Alternate solution, using protocol and an helper constructor
Whatever the solution you can use it the same way
A Go solution similar to the Python one:
Learning a bit of c++:
#ifndef CSTACK_H
#define CSTACK_H
using namespace std;
struct node {
int x;
node *next;
};
class CStack {
public:
CStack();
CStack(const CStack& orig);
virtual ~CStack();
void push(int x);
int pop();
int minimum();
private:
int min;
node *head;
};
#endif /* CSTACK_H */
#include “CStack.h”
#include
#include
using namespace std;
CStack::CStack() {
min=1000;
}
CStack::CStack(const CStack& orig) {
}
CStack::~CStack() {
}
int CStack::minimum() {
return min;
}
void CStack::push(int x) {
node *current;
if(head != NULL){
current = new node;
current->x=x;
if(min>x){
min=x;
}
current->next=head;
head=current;
}
else{
head = new node;
min=x;
head->x=x;
}
}
int CStack::pop() {
int ret = head->x;
head=head->next;
return ret;
}
[…] Pages: 1 2 […]
Perl6 version:
[…] problem: Min Stack Design a data structure that provides push and pop operations, like a stack, plus a third operation […]
should rename the method name as http://swuecho.wordpress.com/2012/08/04/min-stack/, because of names conflict.