Java - stack containing min function

topic link

Niuke online oj question - stack containing min function

topic description

To define the data structure of the stack, please implement a min function in this type that can get the smallest element contained in the stack. When inputting operations, ensure that there must be elements in the stack when the pop, top, and min functions operate.

The methods contained in this stack are:
push(value): push value into the stack
pop(): pop the top element of the stack
top(): get the top element of the stack
min(): get the smallest element in the stack

Data range: the number of operations satisfies 0≤n≤300, and the input elements satisfy ∣val∣≤10000

Advanced: The time complexity of each operation of the stack is O(1), and the space complexity is O(n)

Example:
Input: ["PSH-1", "PSH2", "MIN", "TOP", "POP", "PSH1", "TOP", "MIN"] Output: -1,2,1,
-1

Analysis:
"PSH-1" means to push -1 into the stack, and the element in the stack is -1
"PSH2" means to push 2 into the stack, and the element in the stack is 2, -1
"MIN" means to get the stack at this time The smallest element ==> return -1
"TOP" means to get the top element of the stack ==> return 2
"POP" means pop the top element of the stack, pop 2, the element in the stack is -1
"PSH1" means push 1 into the stack, The element in the stack is 1, -1
"TOP" means to get the top element of the stack ==> return 1
"MIN" means to get the smallest element in the stack at this time ==> return -1

Topic example

Input:
["PSH-1", "PSH2", "MIN", "TOP", "POP", "PSH1", "TOP", "MIN"]

Return value:
-1,2,1,-1

problem solving ideas

The topic requires constructing a minimum value that can obtain the current stack. It is easy to think of constructing a min variable to store the minimum value. It can be returned when the min method is called, but if the min method is called again after the pop element, the minimum value cannot be obtained.

Therefore, it is conceivable to use tools such as arrays for sorting, and update the value of the array after the stack pops out the elements. In fact, the space complexity of doing so is at least o(N)

A more advanced approach is: create an auxiliary stack, and a data stack, the data stack is used to store the data in the stack, and the auxiliary stack stores the minimum value in the stack

At the beginning, the stack is empty. When inserting an element, insert the element into the auxiliary stack and the data stack at the same time. When the stack is not empty, inserting an element needs to compare the size of the element and the top of the auxiliary stack. If the element is smaller than the auxiliary stack, Insert the element into the auxiliary stack, otherwise insert the top element of the auxiliary stack, and then insert the data into the data stack

When the stack performs a pop operation, both the auxiliary stack and the data stack need to be popped, and the top of the data stack is returned when the peek operation is executed, and the top of the auxiliary stack is returned when the min operation is executed.

For example:
execute push(-1) operation, the current stack is empty, directly insert into minStack and dataStack and
insert image description here
execute push(2) operation, the current top element of minStack stack is -1, less than 2, insert -1 into minStack, and insert into dataStack Insert 2
insert image description here
to execute the min operation, and return the top element of the current minStack stack -1

Execute the top operation and return the top element 2 of the current dataStack

Perform pop operation, pop minStack and dataStack at the same time and
insert image description here
perform push(1) operation, the current minStack top element is -1, less than 1, minStack inserts -1, dataStack inserts 1,
insert image description here
executes top operation, returns dataStack top element 1

Execute the min operation and return the minStack top element -1

full code

import java.util.Stack;

public class Solution {
    
    
    private Stack<Integer> dataStack = new Stack<>();
    private Stack<Integer> minStack = new Stack<>();

    public void push(int node) {
    
    
        if(minStack.isEmpty() || minStack.peek() > node){
    
    
            minStack.push(node);
        } else {
    
    
            minStack.push(minStack.peek());
        }
        dataStack.push(node);
    }

    public void pop() {
    
    
        minStack.pop();
        dataStack.pop();
    }

    public int top() {
    
    
        return dataStack.peek();
    }

    public int min() {
    
    
        return minStack.peek();
    }
}

Guess you like

Origin blog.csdn.net/m0_60867520/article/details/130271056