设计一个栈,出pop与push方法,还支持 min方法,可返回栈元素中的最小值

设计一个栈,除pop与push方法,还支持min方法,可返回栈元素中的最小值。java

push,pop和min三个方法的时间复杂度必须为O(1)ide

要保证min的时间复杂度为O(1), 那就不能每次经过遍历整个栈来获取最小值。假设这个栈初始为5:this

5; 最小值为5; 压入7,栈变为spa

7,5;最小值为5; 压入3,栈变为设计

3,7,5; 最小值为3code

 

这时弹出3,栈变为对象

7,5; 最小值为5; 弹出7, 栈变为it

5; 最小值为5;class

 

注意在7,5这个状态时,最小值都是5. 也就是说,若是咱们能标记出栈的每一个状态,那么能够直接取出最小值。能够用一个栈来保存最小值。test

stack类的peek()方法,查看栈顶对象而不移除它

  1. /** 
  2.  * 思路:每一个结点记录当前最小值。 
  3.  * 缺点:当栈很大时,每一个元素都要记录 min,就会浪费大量空间。 
  4.  */  
  5. public class StackWithMin extends Stack<NodeWithMin>{  
  6.   
  7.       public static void main(String[] args) {  
  8.              // TODO Auto-generated method stub  
  9.   
  10.       }  
  11.         
  12.       public void push(int value){  
  13.              int newMin=Math. min(value , min());  
  14.              super.push( new NodeWithMin( value, newMin));              
  15.       }  
  16.         
  17.       public int min(){  
  18.              if( this.isEmpty())  
  19.                    return Integer. MAX_VALUE;  
  20.              else  
  21.                    return peek(). min;  
  22.       }  
  23. }  
  24.   
  25. class NodeWithMin{  
  26.       public int value;  
  27.       public int min;  
  28.         
  29.       public NodeWithMin( int value, int min){  
  30.              this. value= value;  
  31.              this. min= min;  
  32.       }  
  33. }

对于栈,实现其min方法很简单,就是每次push一个值的时候,对minValue进行维护:

if(value < minVlaue){
    minValue = value;
}
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

每次调用min() 时直接返回minValue便可。可是,若是pop走的是minValue则须要遍历剩下的元素求最小值。因此这样没法知足O(1)的要求,咱们须要额外的空间保存栈的每个状态下的最小值。

正如上面陈述的,只要当push的值小于minValue时和pop的值等于minValue时才须要进行额外的操做,用额外的一个栈 stackOfMin来记录最小值的状况。当push的值小于minValue时,stackOfMin push新的最小值;pop的值等于minValue时,stackOfMin也相应地pop就行。

代码

 //存放最小值的栈   //调用自身Stack的Push方法,而不是StackWithMin2的方法(sMin是Stack类)   package test1;

import java.util.Stack;

public class StackWithMin extends Stack<Integer>{

    private Stack<Integer> stackOfMin;

    public StackWithMin() {stackOfMin = new Stack<>();
    }

    public int min() {
        if(stackOfMin.isEmpty()){
            return Integer.MAX_VALUE;
        }else {
            return stackOfMin.peek();
        }
    }

    @Override
    public Integer push(Integer item) {
        if(item < min()){
            stackOfMin.push(item);}
        return super.push(item);
    }

    @Override
    public synchronized Integer pop() {
        if(this.peek() == min()){
            stackOfMin.pop();
        }
        return super.pop();
    }

    public static void main(String[] args) {
        StackWithMin stackWithMin = new StackWithMin();
        stackWithMin.push(19);
        System.out.println("The min is: "+stackWithMin.min());

        stackWithMin.push(15);
        System.out.println("The min is: "+stackWithMin.min());

        stackWithMin.push(17);
        System.out.println("The min is: "+stackWithMin.min());

        stackWithMin.pop();
        System.out.println("The min is: "+stackWithMin.min());

        stackWithMin.pop();
        System.out.println("The min is: "+stackWithMin.min());

        stackWithMin.push(30);
        System.out.println("The min is: "+stackWithMin.min());

        stackWithMin.push(1);
        System.out.println("The min is: "+stackWithMin.min());

        stackWithMin.push(4);
        System.out.println("The min is: "+stackWithMin.min());
    }

}

 

The min is: 19
The min is: 15
The min is: 15
The min is: 15
The min is: 19
The min is: 19
The min is: 1
The min is: 1
相关文章
相关标签/搜索