Maintain a variable min which will contain the minimum element.

push(n): if(n < min)

{

stack.push( n - (min - n) );

min = n

}

else

{

stack.push(n)

}

pop(): retVal = stack.pop();

if( retVal < min)

{

temp = min;

min = min + ( min - retVal);

retVal = temp

}

return retVal;

findMin(): return min;

push(n): if(n < min)

{

stack.push( n - (min - n) );

min = n

}

else

{

stack.push(n)

}

pop(): retVal = stack.pop();

if( retVal < min)

{

temp = min;

min = min + ( min - retVal);

retVal = temp

}

return retVal;

findMin(): return min;

## No comments:

## Post a Comment