I am trying to implement kind of Stack, which has push(), pop(), getMaxSoFar(). It should be executed o(1) time. However, I got an error in push(T value), and I don't know why. The error message said the operator ">=" is not defined in the type of T. I just wanted to check the code so I put int type instead of then it worked.
class FastMaxStack<T>
{
private Stack<T> stack;
private Stack<T> maxStack;
public FastMaxStack()
{
stack = new Stack();
maxStack = new Stack();
}
public void push(T value)
{
if(maxStack.isEmpty())
{
maxStack.push(value);
}
else if(value >= maxStack.peek())
{
maxStack.push(value);
}
stack.push(value);
}
public T pop()
{
maxStack.pop();
return stack.pop();
}
public T getMaxSoFar()
{
return maxStack.peek();
}
}