|
ADS
Algorithms and Data Structures
|
The STACK with the minimum of exercise 3.14 of Morin's book, beyond the book [see ADT.h]: push(), pop(), top(), size() and min(), the least item inside, all in \( O( 1 ) \) amortized time (worst case but for the doublings of the vectors). More...
#include "ADS/Contract.h"#include "ADS/Portable.h"#include <algorithm>#include <cstddef>#include <functional>#include <utility>#include <vector>#include "ADS/stack/impl/MinStack.tpp"Go to the source code of this file.
Classes | |
| class | ADS::MinStack< T, C > |
| a STACK with its least item in \( O( 1 ) \) time More... | |
Namespaces | |
| namespace | ADS |
The STACK with the minimum of exercise 3.14 of Morin's book, beyond the book [see ADT.h]: push(), pop(), top(), size() and min(), the least item inside, all in \( O( 1 ) \) amortized time (worst case but for the doublings of the vectors).
Beside the vector of the items, a second vector keeps the minima: an item goes there too when it is not larger than the minimum on its top, and leaves it with the item; hence the top of the minima is the least item of the stack, since every item above the item that put it there is not smaller. The minima are one item per record low from the bottom, \( n \) at most (a decreasing input) and far fewer on most inputs, where the pairs of an item and its minimum would store \( 2 n \) items.
The order is that of C, a strict weak order, std::less by default; an item equal to the minimum goes to the minima too, so that removing it leaves the minimum of the others. The contracts [see Contract.h]: top(), pop() and min() need a stack that is not empty; the invariant is that the minima do not increase from the bottom and that their top is the least item, an \( O( n ) \) check. The operations are defined in MinStack.tpp.