相关疑难解决方法(0)

设计一个堆栈,使得getMinimum()应为O(1)

这是一个面试问题.您需要设计一个包含整数值的堆栈,以便getMinimum()函数返回堆栈中的最小元素.

例如:考虑下面的例子

case #1

5  --> TOP
1
4
6
2

When getMinimum() is called it should return 1, which is the minimum element 
in the stack. 

case #2

stack.pop()
stack.pop()

Note: Both 5 and 1 are poped out of the stack. So after this, the stack
looks like,

4  --> TOP
6
2

When getMinimum() is called is should return 2 which is the minimum in the 
stack.

制约性:

  1. getMinimum应返回O(1)中的最小值
  2. 在设计时也必须考虑空间约束,如果使用额外的空间,它应该是恒定的空间.

language-agnostic algorithm stack data-structures

116
推荐指数
5
解决办法
10万
查看次数