Download
stack n.
Skip this Video
Loading SlideShow in 5 Seconds..
Stack PowerPoint Presentation

Stack

155 Vues Download Presentation
Télécharger la présentation

Stack

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Stack

  2. Problem • How would you design a stack which, in addition to push and pop, also has a function min which returns the minimum element? Push, pop and min should all operate in O(1) time.

  3. Hints • You can implement this by having each node in the stack keep track of the minimum beneath itself. Then, to find the min, you just look at what the top element thinks is the min. • When you push an element onto the stack, the element is given the current minimum. It sets its “local min” to be the min.

  4. public class StackWithMin extends Stack<NodeWithMin> { public void push(int value) { } public int min() { } }