Design a stack. Besides the pop and push methods, the min method is also supported. The minimum values of the stack elements can be returned. The time complexity of the push, pop, and Min methods must be O (1)

Source: Internet
Author: User
1/* 2 * design a stack. Besides the pop and push methods, the min method is also supported to return the minimum values in the stack elements, 3 * the time complexity of the push, pop, and Min methods must be O (1) 4 * A solution is to add an int-type minvalue to the stack class, when minvalue goes out of the stack, we will search for the entire stack 5 * To find the latest minimum value, but it does not meet the O (1) operation time requirement 6 * if there are: 7 * Push (4) // minimum value: 4 8 * Push (5) // minimum value: 4 9 * Push (3) // minimum value: 310 * Push (1) // minimum value: 111 * Pop () // minimum value 312 * from the above example, we can easily find the minimum value by keeping the minimum value of each State down. 13 * each node records the current minimum value. If you find Min, you can directly view the top element of the stack to find the minimum value. 14 * In this program, use the exception stack to record these min15 * 16 **/17 18 Import Java. util. stack; 19 20 public class stackmin extends stack <integer> {21 22 stack <integer> S; 23 public stackmin () 24 {25 s = new stack <integer> (); 26} 27 public int min () 28 {29 If (S. isempty () 30 return integer. max_value; 31 else 32 return S. peek (); 33} 34 public void push (INT value) 35 {36 IF (value <= min () // compare the current value with the minimum value, if the current value is smaller than the minimum value, the current value is pushed to the stack for 37 s. push (value); 38 super. push (value); 39} 40 public integer POP () 41 {42 int value = super. pop (); 43 If (value = min () // if the current value is out of the stack and is the minimum value, 44 {45 s should be popped up at the minimum value saved in the stack. pop (); 46} 47 return value; 48} 49 public static void main (string [] ARGs) {50 // todo auto-generated method stub51 stackmin stack = new stackmin (); 52 system. out. println ("current minimum value:" + stack. min (); 53 stack. push (5); 54 stack. push (6); 55 stack. push (3); 56 stack. push (7); 57 system. out. println ("current minimum value:" + stack. min (); 58 stack. pop (); 59 stack. pop (); 60 system. out. println ("current minimum value:" + stack. min (); 61 62} 63 64}

 

Design a stack. Besides the pop and push methods, the min method is also supported. The minimum values of the stack elements can be returned. The time complexity of the push, pop, and Min methods must be O (1)

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.