Python implementation time o (1) Minimum stack, python small stack

Source: Internet
Author: User

Python implementation time o (1) Minimum stack, python small stack

This is a handwritten programming question I encountered when I graduated from the school. I was just learning python and it took a lot of time to write down the entire stack. After all, the language is just a tool. If you want to implement it clearly, you can use any language to quickly write it out.

What is the minimum stack? The most basic stack operations are push and pop. Now you need to add a function (get_min) that returns the minimum value in the stack ), the time complexity of the get_min function is required to be o (1 ). The python stack must be implemented using the list. As long as the append and pop of the list are encapsulated into the stack class, the stack is pressed and rolled back. Without considering the time complexity, the first response must be min (). min () can return the minimum value in the stack of o (n) without opening up new space. However, if there are many elements in the stack and the get_min method needs to be called frequently, the disadvantages of high min consumption will be magnified. The ideal method is to change the space for time to reduce the time complexity.

Stack_list and min_list exist in our stacks. min_list is responsible for storing the stacks composed of the minimum values of the elements in the stacks. When the elements of the new pressure stacks are smaller than or equal to the minimum elements in the stacks, the new elements are pushed to min_list. If the rollback element is equal to the smallest element in the stack, the min_list should also be rolled back. For example, we press stacks 3, 2, 4, and 1 in sequence.

Initialization

Stack_list = []

Min_list = []

3. Stack pressure

Stack_list = [3]

Min_list = [3]

2. Pressure Stack

Stack_list = [3, 2]

Min_list = [3, 2]

4. Stack pressure

Stack_list = [3, 2, 4]

Min_list = [3, 2]

1. Pressure Stack

Stack_list = [3, 2, 4, 1]

Min_list = [3, 2, 1]

Get_min only needs to return the last element in min_list. The following is the complete implementation of python code.

#!/usr/bin/python# -*- coding: utf-8 -*-class stack(object):    stack_list = []    min_list = []    min = None    def push(self, x):        if not self.stack_list:            self.min = x            self.min_list.append(self.min)            self.stack_list.append(x)            return        self.stack_list.append(x)        if self.min >= x:            self.min = x            self.min_list.append(self.min)        return    def pop(self):        pop_result = None        if self.stack_list:            pop_result = self.stack_list[-1]            if self.stack_list.pop() == self.min:                self.min_list.pop()                if self.min_list:                    self.min = self.min_list[-1]                else:                    self.min = None            return pop_result        else:            self.min = None            return pop_result    def print_stack(self):        print "stack---->", self.stack_list        return    def get_min(self):        return self.min

 

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.