Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
push(x) – Push element x onto stack.
pop() – Removes the element on top of the stack.
top() – Get the top element.
getMin() – Retrieve the minimum element in the stack.
Example:
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); --> Returns -3.
minStack.pop();
minStack.top(); --> Returns 0.
minStack.getMin(); --> Returns -2.
public class _155MinStack2 {

本文介绍了如何设计一个栈,除了支持基本的push、pop、top操作外,还能在常数时间内获取栈中的最小元素。具体实现涉及堆算法和系统堆的使用。
订阅专栏 解锁全文
984

被折叠的 条评论
为什么被折叠?



