思路:
1、利用额外空间处理
2、push时,把最小元素压入栈中,再有新元素进入等于当前最小元素则重复压入,保证压入重复元素是无误
3、poll时,弹出元素,与当前最小值一致则弹出。
代码如下:
package com.my.test.datastructure.stack;
import java.util.Stack;
/*
* 实现一个栈 获取最大、最小值、pop()、push()都是O(1)复杂度
*/
public class MinMaxStack
{
private Stack<Integer> stack = new Stack<>();
private Stack<Integer> minStack = new Stack<>();
private Stack<Integer> maxStack = new Stack<>();
public void push(int value) {
// 插入元素
stack.push(value);
// 第一次push元素直接插入 或者 小于等于栈顶元素,则插入最小栈
if (minStack.empty() || minStack.peek() >= value) {
minStack.push(value);
}
// 第一次push元素 或者 大于等于栈顶元素,则插入最大栈
if (maxStack.empty() || maxStack.peek() <= value) {
maxStack.push(value);
}
}
public int pop() {
if (stack.empty()) {
System.out.println("stack is empty");
throw new RuntimeException("stack is empty");
}
int popValue = stack.pop();
// 元素为最大值或最小值则弹出
if (popValue == minStack.peek()) {
minStack.pop();
}
if (popValue == maxStack.peek()) {
maxStack.pop();
}
return popValue;
}
public int getMin() {
if (minStack.empty()) {
throw new RuntimeException("min stack is empty");
}
return minStack.peek();
}
public int getMax() {
if (maxStack.empty()) {
throw new RuntimeException("max stack is empty");
}
return maxStack.peek();
}
public static void main(String args[]) {
MinMaxStack stack = new MinMaxStack();
stack.push(2);
stack.push(3);
stack.push(1);
stack.push(3);
stack.push(1);
stack.push(4);
stack.push(2);
System.out.println(String.format("min: %s, max: %s", stack.getMin(), stack.getMax()));
stack.pop();
System.out.println(String.format("min: %s, max: %s", stack.getMin(), stack.getMax()));
stack.pop();
System.out.println(String.format("min: %s, max: %s", stack.getMin(), stack.getMax()));
stack.pop();
System.out.println(String.format("min: %s, max: %s", stack.getMin(), stack.getMax()));
stack.pop();
System.out.println(String.format("min: %s, max: %s", stack.getMin(), stack.getMax()));
}
}
一种更加简单的方案:
1、每次push元素,如果比当前最小元素小,则在最小栈中加入当前栈顶元素
2、每次pop()时,直接pop即可
代码如下:
package com.my.test.datastructure.stack;
import java.util.Stack;
/*
* 实现一个栈 获取最大、最小值、pop()、push()都是O(1)复杂度
*/
public class MinMaxStack2
{
private Stack<Integer> stack = new Stack<>();
private Stack<Integer> minStack = new Stack<>();
private Stack<Integer> maxStack = new Stack<>();
public void push(int value) {
// 插入元素
stack.push(value);
// 第一次push元素直接插入 或 小于等于栈顶元素,则插入最小栈
if (minStack.empty() || minStack.peek() >= value) {
minStack.push(value);
}
// 否则再次插入当前栈顶元素
else {
minStack.push(minStack.peek());
}
// 第一次push元素 或 大于等于栈顶元素,则插入最大栈
if (maxStack.empty() || maxStack.peek() <= value) {
maxStack.push(value);
}
// 否则再次插入当前栈顶元素
else {
maxStack.push(maxStack.peek());
}
}
public int pop() {
if (stack.empty()) {
System.out.println("stack is empty");
throw new RuntimeException("stack is empty");
}
// 最小栈 最大栈数据直接弹出
minStack.pop();
maxStack.pop();
return stack.pop();
}
public int getMin() {
if (minStack.empty()) {
throw new RuntimeException("min stack is empty");
}
return minStack.peek();
}
public int getMax() {
if (maxStack.empty()) {
throw new RuntimeException("max stack is empty");
}
return maxStack.peek();
}
public static void main(String args[]) {
MinMaxStack2 stack = new MinMaxStack2();
stack.push(2);
stack.push(3);
stack.push(1);
stack.push(3);
stack.push(1);
stack.push(4);
stack.push(2);
System.out.println(String.format("min: %s, max: %s", stack.getMin(), stack.getMax()));
stack.pop();
System.out.println(String.format("min: %s, max: %s", stack.getMin(), stack.getMax()));
stack.pop();
System.out.println(String.format("min: %s, max: %s", stack.getMin(), stack.getMax()));
stack.pop();
System.out.println(String.format("min: %s, max: %s", stack.getMin(), stack.getMax()));
stack.pop();
System.out.println(String.format("min: %s, max: %s", stack.getMin(), stack.getMax()));
}
}
参考: