面试的时候,面试官让设计一个栈,要求有Push、Pop和获取最大最小值的操作,并且所有的操作都能够在O(1)的时间复杂度完成。
当时真没啥思路,后来在网上查了一下,恍然大悟,只能恨自己见识短浅、思路不够开阔,特地写个总结来学习一下。
其实思路挺简单,只是没有接触过的话,一时反应不过来。我们将栈中的每个元素都增加两个索引号,一个最大元素索引一个最小元素索引,这样我们可以根据栈只能访问栈顶元素的特性,在每个元素入栈时记下当前栈里面的最大最小元素的索引号,这样我们通过对栈顶元素的访问,就可以随时拿到当前栈中最大最小元素了,这是典型的空间换时间思想。
示例代码如下:
#include<iostream> using namespace std; typedef int ElemType; class MinMaxStack
{
public:
MinMaxStack() : m_size() { }
~MinMaxStack() { } bool Push(const ElemType& e)
{
if (m_size >= STACK_MAXIMUM) {
return false;
} // 如果是第一个元素,则将最大最小元素索引都设置为0
if (m_size == ) {
m_stack[m_size].elem = e;
m_stack[m_size].maxIndex = ;
m_stack[m_size].minIndex = ;
}
else {
int minIndex = m_stack[m_size - ].minIndex;
int maxIndex = m_stack[m_size - ].maxIndex;
m_stack[m_size].elem = e; // 设置插入元素的最大元素索引:
// 若插入元素比当前最大元素大,则将其最大元素索引设置为它自己的索引
// 否则,设置为前一个元素的最大元素索引
if (e > m_stack[maxIndex].elem) {
m_stack[m_size].maxIndex = m_size;
}
else {
m_stack[m_size].maxIndex = maxIndex;
} // 设置插入元素的最小元素索引
// 若插入元素比当前最小元素小,则将其最小元素索引设置为它自己的索引
// 否则,设置为前一个元素的最小元素索引
if (e < m_stack[minIndex].elem) {
m_stack[m_size].minIndex = m_size;
}
else {
m_stack[m_size].minIndex = minIndex;
}
}
m_size++; return true;
} bool Pop(ElemType& e)
{
if (m_size == ) {
return false;
} m_size--;
e = m_stack[m_size].elem; return true;
} int Size()
{
return m_size;
} const ElemType& Min()
{
if (m_size == ) {
return INIT_VALUE;
} int minIndex = m_stack[m_size - ].minIndex;
return m_stack[minIndex].elem;
} const ElemType& Max()
{
if (m_size == ) {
return INIT_VALUE;
} int maxIndex = m_stack[m_size - ].maxIndex;
return m_stack[maxIndex].elem;
} private:
struct StackNode
{
StackNode(const ElemType& e = INIT_VALUE, int min = , int max = )
: elem(e), minIndex(min), maxIndex(max)
{
} ElemType elem;
int minIndex;
int maxIndex;
}; static const int STACK_MAXIMUM = ;
static const ElemType INIT_VALUE; StackNode m_stack[STACK_MAXIMUM];
int m_size;
}; const ElemType MinMaxStack::INIT_VALUE = -; // 测试代码
int main()
{
MinMaxStack stack;
stack.Push();
stack.Push();
stack.Push();
stack.Push();
stack.Push();
stack.Push();
stack.Push();
stack.Push(); cout << "size = " << stack.Size() << endl;
cout << "min = " << stack.Min() << endl;
cout << "max = " << stack.Max() << endl;
cout << "------------------------------------" << endl; ElemType e;
stack.Pop(e);
cout << "size = " << stack.Size() << endl;
cout << "min = " << stack.Min() << endl;
cout << "max = " << stack.Max() << endl;
cout << "pop = " << e << endl;
cout << "------------------------------------" << endl; stack.Pop(e);
cout << "size = " << stack.Size() << endl;
cout << "min = " << stack.Min() << endl;
cout << "max = " << stack.Max() << endl;
cout << "pop = " << e << endl;
cout << "------------------------------------" << endl; stack.Pop(e);
cout << "size = " << stack.Size() << endl;
cout << "min = " << stack.Min() << endl;
cout << "max = " << stack.Max() << endl;
cout << "pop = " << e << endl;
cout << "------------------------------------" << endl; stack.Pop(e);
cout << "size = " << stack.Size() << endl;
cout << "min = " << stack.Min() << endl;
cout << "max = " << stack.Max() << endl;
cout << "pop = " << e << endl;
cout << "------------------------------------" << endl; stack.Pop(e);
cout << "size = " << stack.Size() << endl;
cout << "min = " << stack.Min() << endl;
cout << "max = " << stack.Max() << endl;
cout << "pop = " << e << endl;
cout << "------------------------------------" << endl; return ;
}