Microsoft algorithm 100 question 02 design stack containing min functions, 02 min

Source: Internet
Author: User

Microsoft algorithm 100 question 02 design stack containing min functions, 02 min

Requirements:
1. Define the stack data structure and add a min function to obtain the minimum element of the stack.
2. the time complexity of the min, push, and pop functions is required to be O (1)

 

Idea: Build an auxiliary stack. Only when the data in the current stack is smaller than the top element of the stack of the auxiliary stack, push it to the auxiliary stack to ensure that the top element of the stack of the auxiliary stack is always the minimum, when an outbound stack element is greater than the top element of the secondary stack, the element must not be in the secondary stack, because the auxiliary stack retains the order of the original Stack's inbound stack (only some elements are discarded by storage ), this large element has been filtered out by the auxiliary stack when it is imported into the stack. If the element of the output stack is smaller than or equal to the element at the top of the auxiliary stack, the auxiliary Stack also performs pop operations.

 

1 package com. rui. microsoft; 2 3 import java. util. stack; 4 5 public class Test02_MinStack {6 7 public static void main (String [] args) {8 MyStack myStack = new MyStack (); 9 myStack. push (5); 10 myStack. push (3); 11 myStack. push (1); 12 myStack. push (6); 13 14 System. out. println (myStack. min (); 15 16 myStack. pop (); 17 System. out. println (myStack. min (); 18 19 myStack. pop (); 20 System. out. println (myStack. min (); 21 22} 23 24 static class MyStack {25 private Stack <Integer> stack = new Stack <Integer> (); 26 private Stack <Integer> minStack = new Stack <Integer> (); 27 28 public void push (int I) {29 stack. push (I); 30 31 if (minStack. isEmpty () {32 minStack. push (I); 33} else {34 int min = minStack. peek (); 35 // minStack only pushes item smaller than its top item36 // elements 37 if (I <min) {38 minStack whose size is smaller than its top Element. push (I); 39} 40} 41} 42 43 public Integer pop () {44 Integer I = stack. pop (); 45 // when pop an item from original stack46 // we need to process the minStack also47 // if the item popped from original stack is larger than minStack's top item48 // it means this item shoshould not exist in minStack49 if (I <= minStack. peek () {50 minStack. pop (); 51} 52 return I; 53} 54 55 public Integer min () {56 return minStack. peek (); 57} 58} 59}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.