Python implements the minimum stack of Time O (1)

Source: Internet
Author: User

This is the graduation school recruit two face of handwriting programming problem, at that time just began to learn python, the whole stack write down also spent a lot of time. After all, language is just a tool, and if you want to make it clear, use any language to write it quickly.

What is the minimum stack? The most fundamental operation of the stack is the stack (push) and the stack (POP), which now needs to add a function (Get_min) that returns the minimum value in the stack, requiring the time complexity of the get_min function to be O (1). The python stack must be implemented using the list, as long as the list's Append and POPs are encapsulated in the stack class, which means that the stack and stack are implemented. Without considering the complexity of the time, our first reaction must be min (), min () can return the minimum value of O (n) in the case of no new space. However, if there are many elements in the stack, and the Get_min method needs to be called frequently, the disadvantage of min high time-consuming is magnified, so the ideal method is to reduce the time complexity by changing the space time.

There are stack_list and min_list,min_list in our stack that are responsible for storing the minimum value of the elements in the stack, and when the new stack element is less than the smallest element in the stack, the new element is pressed into the min_list. If the stack element is equal to the smallest element in the stack, then the min_list is also retired. For example, we press the stack 3,2,4,1 in turn

Initialization

Stack_list = []

Min_list = []

3 Press Stack

Stack_list = [3]

Min_list = [3]

2 Press Stack

Stack_list = [3, 2]

Min_list = [3, 2]

4 Press Stack

Stack_list = [3, 2, 4]

Min_list = [3, 2]

1 Press Stack

Stack_list = [3, 2, 4, 1]

Min_list = [3, 2, 1]

Get_min only needs to return the last element in the Min_list, the following is the complete implementation of the Python code

#!/usr/bin/python#-*-coding:utf-8-*-classStack (object): Stack_list=[] min_list=[] min=Nonedefpush (self, x):if  notSelf.stack_list:self.min=x self.min_list.append (self.min) self.stack_list.append (x)returnself.stack_list.append (x)ifSelf.min >=X:self.min=x self.min_list.append (self.min)return    defpop (self): Pop_result=NoneifSelf.stack_list:pop_result= Self.stack_list[-1]            ifSelf.stack_list.pop () = =Self.min:self.min_list.pop ()ifSelf.min_list:self.min= Self.min_list[-1]                Else: Self.min=NonereturnPop_resultElse: Self.min=NonereturnPop_resultdefPrint_stack (self):Print "stack---->", Self.stack_listreturn    defget_min (self):returnSelf.min

Python implements the minimum stack of Time O (1)

Related Article

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.