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; }
}