table of Contents
Constraint 1: Requires algorithm time complexity o(1)
Constraint 1: Use agency design pattern, decorator design pattern
2. Java implements the algorithm problem
1. Title description
It is required to obtain the minimum value of the current stack in the stack. The subject has the following constraints:
Constraint 1: The time complexity of the algorithm is O(1)
Constraint 1: Use agency design pattern, decorator design pattern
2. Java implements the algorithm problem
package com.autocoding.stack;
import java.util.Arrays;
import java.util.Random;
import java.util.Stack;
/**
* 一道算法面试题:要求对栈Stack中获取当前栈最小值,要求算法时间复杂度o(1)
* <p>
* 这里使用了两个设计模式:
* 1、静态代理设计模式:StackWrapper对Stack进行了继承式代理,控制了Stack对象push()、pop()方法的访问
* 2、装饰者设计模式:对Stack进行了方法的增强,增加了一个getMin()方法,对Stack对象进行了装饰
* </p>
* @ClassName: StackWrapper
* @Description: 用一句话描述该文件做什么
* @author: QiaoLi
* @date: Jan 21, 2021 11:14:26 AM
* @param <T>
*/
public class StackWrapper<T extends Comparable<T>> extends Stack<T> {
private static final long serialVersionUID = 1L;
private Stack<T> minValueStack = new Stack<T>();
public T getMin() {
if (this.minValueStack.empty()) {
return null;
}
return this.minValueStack.peek();
}
@Override
public T push(T item) {
if (this.minValueStack.empty()) {
this.minValueStack.push(item);
} else {
T curMinValue = this.minValueStack.peek();
if (curMinValue.compareTo(item) > 0) {
this.minValueStack.push(item);
} else {
this.minValueStack.push(curMinValue);
}
}
return super.push(item);
}
@Override
public synchronized T pop() {
this.minValueStack.pop();
return super.pop();
}
public static void main(String[] args) {
StackWrapper<Integer> stackWrapper = new StackWrapper<Integer>();
Random random = new Random();
for (int i = 0; i < 10; i++) {
Integer randomValue = random.nextInt(100);
System.out.println("入栈:" + randomValue);
stackWrapper.push(randomValue);
}
for (int i = 0; i < 10; i++) {
Integer topValue = stackWrapper.pop();
Integer[] array = new Integer[stackWrapper.size()];
if (!stackWrapper.empty()) {
Arrays.sort(stackWrapper.toArray(array));
}
System.err.println("出栈:" + topValue + "之后" + ",栈:" + Arrays.toString(array));
System.err.println("最小值:" + stackWrapper.getMin());
}
}
}
3. The results of the program
入栈:25
入栈:45
入栈:53
入栈:94
入栈:33
入栈:21
入栈:28
入栈:30
入栈:92
入栈:20
出栈:20之后,栈:[21, 25, 28, 30, 33, 45, 53, 92, 94]
最小值:21
出栈:92之后,栈:[21, 25, 28, 30, 33, 45, 53, 94]
最小值:21
出栈:30之后,栈:[21, 25, 28, 33, 45, 53, 94]
最小值:21
出栈:28之后,栈:[21, 25, 33, 45, 53, 94]
最小值:21
出栈:21之后,栈:[25, 33, 45, 53, 94]
最小值:25
出栈:33之后,栈:[25, 45, 53, 94]
最小值:25
出栈:94之后,栈:[25, 45, 53]
最小值:25
出栈:53之后,栈:[25, 45]
最小值:25
出栈:45之后,栈:[25]
最小值:25
出栈:25之后,栈:[]
最小值:null
4. Summary
For some performance optimization problems, after trying various methods, the problem can only be solved by optimizing the algorithm. For example, there is no other better way to solve the problem of keyword filtering. You can only use the DFA algorithm. Please refer to my other document. : Record an application case of the sensitive word filtering algorithm DFA