锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
鎴戠殑瑙?br>
interface IntStack
{
int pop();
void push(int i);
int get();
}
class MinStack
{
//store all the element
private IntStack elemStack = new IntStack();
//store current and historical smallest element
private IntStack minStack = new IntStack();
public void push(int i)
{
elemStack.push(i);
int currentMin = minStack.get();
if(i <= currentMin) minStack.push(i);
}
public int pop()
{
int result = elemStack.pop();
if(result == minStack.get()) minStack.pop();
return result;
}
public int getMinElem()
{
return minStack.get();
}
}
]]>
eg 6
/ \
4 8
/ \ / \
3 5 7 9
3=4=5=6=7=8=9
鎴戠殑瑙?