Monday, December 8, 2014

Min Stack

Problem

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.

Idea

两个stack,一个用来存最小的stack,一个用来存正常的stack

Solution


No comments:

Post a Comment