Min Stack (Java Implementation)

Source: Internet
Author: User

Title: Design a stack that supports push, POPs, top, and retrieving the minimum element at constant time.
Push (x) –push element x onto stack.
Pop () –removes the element on top of the stack.
Top () –get is the top element.
Getmin () –retrieve the minimum element in the stack.

Translation: Design a stack containing push, pop, top, getmin functions, each feature details as follows:
Push (x) – Press the data x into the stack;
Pop () – Eject the data;
Top () – Get stack data;
Getmin () – Gets the smallest element in the stack.

train of thought one -failure method
Failure Method 1: Using an array to implement the stack, but after submitting the error: Error:constructor Minstack in class Minstack cannot is applied to given types;
possible reasons : Because the stack is implemented with arrays, the stack size needs to be given when the stack is initialized, possibly because it does not match the requirements of the topic.
Failure Method 2: Replace the array with ArrayList on the basis of Method 1, and do not report the above error, but always show timeouts.
possible cause : When implementing the Getmin () function, you need to traverse the ArrayList, which can take a long time

idea two – achievable
Define two stacks (can be used ArrayList, can also be implemented using stack), a normal storage data, recorded as data, a only when the need to save the information is less than or equal to the top of the stack of data to deposit, recorded as Mindata; when the stack, data can be directly out of the stack, The mindata pops up only when the top element of the data stack is equal to the top element of the Mindata stack, which ensures that the top element in the Mindata is always the smallest data for the entire stack.

For example:
Push (4) –>data={4}; MINDATA={4};
Push (4) –>data={4,4}; mindata={4,4};
Push (5) –>data={4,4,5}; mindata={4,4};
Push (3) –>data={4,4,5,3}; mindata={4,4,3};
Pop () –> data={4,4,5}; mindata={4,4};
Pop () –>data={4,4}; mindata={4,4};

code (Java)– This is a stack implementation, with ArrayList, but slightly more bloated than stack

Import java.util.*;
Class Minstack {      
    stack<integer> data = new stack<integer> ();
    stack<integer> mindata = new stack<integer> ();

    public void push (int x) {
        data.push (x);
        if (Mindata.isempty () | | x <= Mindata.peek ()) {
             mindata.push (x);
        }
    }

    public void POPs () {
        if (!data.isempty ()) {
            int top = Data.pop ();
            if (top = =  Mindata.peek ()) {
                mindata.pop ();
            }
    }} public int Top () {
        if (!data.isempty ()) {return
            data.peek ();
        } else{return
            0;
        }

    public int getmin () {
        if (!mindata.isempty ()) {return
            mindata.peek ();
        } else{return
            0;}}

Looking for another way of thinking code

class minstack {Node top = null;
            public void push (int x) {if (top = null) {top = new Node (x);
        Top.min = x;
            else {Node temp = new node (x);
            Temp.next = top;
            top = temp;
        Top.min = Math.min (top.next.min, x);
        } public void POPs () {top = Top.next;
    Return
    public int Top () {return top = null? 0:top.val;
    public int getmin () {return top = null? 0:top.min;
    Class Node {int val;
    int min;
    Node Next;
    Public Node (int val) {this.val = val; }
}

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.