leetcode#155 Min Stack

Source: Internet
Author: User
Tags diff

Problem Definition:

Design a stack that supports push, pop, top, and retrieving the minimum element in constanttime.

    • Push (x)-push element x onto stack.
    • Pop ()--Removes the element on top of the stack.
    • Top ()--Get the top element.
    • Getmin ()--Retrieve The minimum element in the stack.

sulotion 1: an additional stack to store the minimum value. Only those new minimum values are stored, each pop is checked, and if the stack element is the same as the top element of the stack stack, the additional stack pops up.

1 classMinstack:2     3     def __init__(self):4self.nums=[]5self.min=[Sys.maxint]6   7     defpush (self, x):8Self.nums+=x,#a little trick,x turns into a tuple, equivalent to append (x)9         ifX<=self.min[len (Self.min)-1]:#Note that the repeat min is also going to stackTenself.min+=x, One       A     defpop (self): -p=Self.nums.pop () -         ifP==self.min[len (self.min)-1]: the Self.min.pop () -  -     defTop (self): -         returnSelf.nums[len (self.nums)-1] +  -     defgetmin (self): +         returnSelf.min[len (Self.min)-1]

This approach to space-time tradeoff is common, and here's a cool real now, constant space complexity:

Solution 2:

1 classMinstack:2 3     def __init__(self):4Self.min =Sys.maxint5Self.diffs = []6 7     defpush (self, x):8Self.diffs + = x-Self.min,9Self.min =min (self.min, x)Ten  One     defpop (self): Adiff =Self.diffs.pop () -Self.min-=min (diff, 0) -  the     defTop (self): -         returnSelf.min + MAX (self.diffs[-1], 0) -  -     defgetmin (self): +         returnSelf.min

Now let's see the tricks here:

1) The diffs array holds the difference between the element's input stack and the minimum value at that time, which may be positive or negative; Min records the current minimum value.

2) Push: The above difference x-min into the stack, if the number of press-in is greater than or equal to 0, then min remains the same, otherwise the min=x is the new minimum value.

3) Pop: first, the top element of the stack is deleted. Then consider whether you want to change the min:

1._ if the top element of the stack is non-negative, it means that the element is not less than min at the time of the stack, so this element does not change (update) min when it is in the stack, so it does not need to change (restore) min when it is deleted, that is, execute min-=0;

2._ if the top element of the stack is negative, it means that the element is less than min at the time of the stack, so this element is updated at the time of the stack, so it is necessary to restore min when it is deleted, that is, to execute min-=diff;

4) Top:1._ If the top element of the stack is non-negative, it means that the element is not less than min at the time of the stack, so its stack does not change min, so it returns MIN+DIFFS[-1]

2._ if the top element of the stack is negative, the element updates min when it is in the stack, and min stores the value of the element when it is in the stack, so it returns min.

leetcode#155 Min Stack

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.