30_ wins the min function stack comprising offer_java_

table of Contents

Title Description

Test Case

Topic test sites

Problem-solving ideas

Reference solving


Title Description

 

Defined stack data, implement a function to find the min smallest element in the stack that type. In this stack, call min, push and pop time complexity is O (. 1) .

 

Test Case

 

  • The new figures pushed onto the stack larger than the minimum before.
  • The new figures pushed onto the stack is smaller than the previous minimum.
  • Pop the stack is not a minimum number of elements.
  • Pop the stack number is the smallest element.

 

Topic test sites

 

  • Thinking examine candidates analyze complex problems - analysis examples.
  • Examine the candidates understanding of the stack.
  • Stack: After a first-in, first-out after

 

Problem-solving ideas

 

Here only limits the time complexity, not the complexity of space limitations , we might think of using an auxiliary stack ( spatial angle).

As shown in the table, using the auxiliary stack, each stack of the minimum value of the current into the secondary press stack, the stack has to ensure that the auxiliary stack is the smallest element . When data is popped from the stack minimum element while the auxiliary stack pop stack elements, in which case the top element of the stack is the next auxiliary stack minimum.

Specific performance function : push, pop, min

. 1, min : take the auxiliary stack top element;

2, Push: if the current element is pressed into the press-fitting is smaller than the minimum value of the auxiliary stack and then pushes otherwise a minimum current (for convenience of pop);

3, POP: direct pop the top element data stack and auxiliary stack.

Stack data, auxiliary stack, the minimum state analysis
step operating Data stack The auxiliary stack (stack each time the current minimum press-fitted) A minimum value (auxiliary stack Stack)
1 Pushed 3 3 3 3
2 Pushed 4 3,4 3,3 3
3 Pushed 2 3,4,2 3,3,2 2
4 Pushed 1 3,4,2,1 3,3,2,1 1
5 pop up 3,4,2 3,3,2 2
6 pop up 3,4 3,3 3
7 Pushed 0 3,4,0 3,3,0 0

 

Reference solving

import java.util.Stack;

public class Solution {

    // 数据栈
    Stack<Integer> dataStack = new Stack<>();
    // 辅助栈
    Stack<Integer> minStack = new Stack<>();
    
    public void push(int node) {
        // 如果压入的元素比当前最小值小则压入辅助栈,
        // 不然再压入一个当前最小值(为了pop的方便);
        dataStack.push(node);
        if(minStack.empty() || node < minStack.peek()){ //empty,true当且仅当此堆栈不包含项目时; 
            minStack.push(node);
        }else{
            minStack.push(minStack.peek());
        }
    }
    
    public void pop() {
        // 直接弹出数据栈与辅助栈的栈顶元素。
        dataStack.pop();
        minStack.pop();
    }
    
    public int top() {
        return dataStack.peek();
    }
    
    public int min() {
        // 取辅助栈栈顶元素
        return minStack.peek();
    }
}

 

 

Published 29 original articles · won praise 0 · Views 1917

Guess you like

Origin blog.csdn.net/Longtermevolution/article/details/104070140