Lintcode12 Min Stack solution problem solution
Topic description]
Implement a stack with min () function, which will return the smallest number in the stack.
It should support push, pop and min operation all in O (1) cost.
Notice:min operation will never be called if there is no number in the stack.
Implement a stack with the min method that takes the minimum value, and the min method returns the minimum value in the current stack.
The stack you implement will support push,pop and min operations, and all operations will be completed in O (1) time.
Note: if there are no numbers in the stack, you cannot call the min method.
[topic link]
Http://www.lintcode.com/en/problem/min-stack/
[topic Analysis]
Two stack structures are used, one of which is the main normal stack, which meets the O (1) time requirements of pop () and push (), and the other, as an auxiliary minStack, is only stored in the integer of min. Min = Integer.parseInt (minStack.peek () .toString ())
When push (), if number > = min, then push to pop () on minStack, if number = = min, also pop from minStack
For the example in the question, the final stack is [2,3,1] and the minStack is [2,1].
[answer link]
Http://www.jiuzhang.com/solutions/min-stack/