Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中搜索堆栈中最大元素的最快方法是什么?_Java_Search_Data Structures_Stack - Fatal编程技术网

在Java中搜索堆栈中最大元素的最快方法是什么?

在Java中搜索堆栈中最大元素的最快方法是什么?,java,search,data-structures,stack,Java,Search,Data Structures,Stack,我有一个问题,我必须搜索堆栈中的最大元素。我创建了自己的堆栈类,并使用了以下方法: Node node = top; //created a new node which points to the top of stack int max = node.data; //max contains the value of the top node while(node != null) { if(node.data > max) {

我有一个问题,我必须搜索堆栈中的最大元素。我创建了自己的堆栈类,并使用了以下方法:

   Node node = top; //created a new node which points to the top of stack

   int max = node.data;  //max contains the value of the top node

   while(node != null) {
           if(node.data > max) {
              max = node.data;
           }
           node = node.next;
    }

   //Print the value of max.
有人能提出更有效的方法吗?

保持两层:

  • 由所有节点组成
  • 始终将Max节点保持在其顶部,这样每次都可以更轻松地获取Max元素
  • 代码如下所示:

    import java.util.Stack;
    
    public class StackWithMax extends Stack<Integer> {
    
    Stack<Integer> s2;
    
    public StackWithMax() {
        s2 = new Stack<Integer>();      
    }
    
    public void push(int value){
        if (value >= max()) {
            s2.push(value);
        }
        super.push(value);
    }
    
    public Integer pop() {
        int value = super.pop();
        if (value == max()) {
            s2.pop();           
        }
        return value;
    }
    
    public int max() {
        if (s2.isEmpty()) {
            return Integer.MIN_VALUE;
        } else {
            return s2.peek();
        }
      }
    } 
    
    import java.util.Stack;
    公共类StackWithMax扩展堆栈{
    堆栈s2;
    公共StackWithMax(){
    s2=新堆栈();
    }
    公共无效推送(int值){
    如果(值>=max()){
    s2.推送(值);
    }
    super.push(值);
    }
    公共整数pop(){
    int value=super.pop();
    如果(值==max()){
    s2.pop();
    }
    返回值;
    }
    公共int max(){
    if(s2.isEmpty()){
    返回Integer.MIN_值;
    }否则{
    返回s2.peek();
    }
    }
    } 
    
    维护两个堆栈:

  • 由所有节点组成
  • 始终将Max节点保持在其顶部,这样每次都可以更轻松地获取Max元素
  • 代码如下所示:

    import java.util.Stack;
    
    public class StackWithMax extends Stack<Integer> {
    
    Stack<Integer> s2;
    
    public StackWithMax() {
        s2 = new Stack<Integer>();      
    }
    
    public void push(int value){
        if (value >= max()) {
            s2.push(value);
        }
        super.push(value);
    }
    
    public Integer pop() {
        int value = super.pop();
        if (value == max()) {
            s2.pop();           
        }
        return value;
    }
    
    public int max() {
        if (s2.isEmpty()) {
            return Integer.MIN_VALUE;
        } else {
            return s2.peek();
        }
      }
    } 
    
    import java.util.Stack;
    公共类StackWithMax扩展堆栈{
    堆栈s2;
    公共StackWithMax(){
    s2=新堆栈();
    }
    公共无效推送(int值){
    如果(值>=max()){
    s2.推送(值);
    }
    super.push(值);
    }
    公共整数pop(){
    int value=super.pop();
    如果(值==max()){
    s2.pop();
    }
    返回值;
    }
    公共int max(){
    if(s2.isEmpty()){
    返回Integer.MIN_值;
    }否则{
    返回s2.peek();
    }
    }
    } 
    
    如果您不介意使用额外的空间,我们可以在O(1)时间内完成getMax()。其思想是使用基于比较器的PriorityQueue,该比较器最多返回两个元素。您的PriorityQueue将由基于比较器按排序方式排列的元素组成。每当在堆栈中推送一个元素时,也会在PriorityQueue中推送与该元素对应的最大元素。让我们举一个例子:

    假设在堆栈中,您正在推送元素3。然后在您的优先队列pQ中,您将提供3。此时,3将是堆栈中与3对应的最大元素

    让我们在堆栈S中插入5个。提供5个pQ。由于5>3,pQ中的元素顺序为5 3。 让我们推出4英寸S。同时提供4英寸pQ。pQ现在将包含元素:5 4 3。如果执行getMax(),则会得到pQ的头部,这需要O(1)个时间,因为最大元素始终位于pQ的顶部

    对于S.pop(),如果以LinkedList的形式存储pQ,则可以在O(1)时间内从pQ中删除相应的弹出元素。因此,所有这些操作实际上需要O(1)个时间

    按照同样的逻辑,您也可以在O(1)时间内完成popMax()。只需返回pQ的头部,并从堆栈中删除相应的节点,这同样可以在O(1)的时间内完成

    以下是两者的结构:

    public class Node{
        int data;
        Node next;
        Node(int data){
            this.data = data;
            next = null;
        }
    }
    
    PriorityQueue<Node> pQ = new PriorityQueue<Node>();
    Stack<Node> S = new Stack<Node>();
    
    公共类节点{
    int数据;
    节点下一步;
    节点(int数据){
    这个数据=数据;
    next=null;
    }
    }
    PriorityQueue pQ=新的PriorityQueue();
    堆栈S=新堆栈();
    
    如果您不介意使用额外的空间,我们可以在O(1)时间内完成getMax()。其思想是使用基于比较器的PriorityQueue,该比较器最多返回两个元素。您的PriorityQueue将由基于比较器按排序方式排列的元素组成。每当在堆栈中推送一个元素时,也会在PriorityQueue中推送与该元素对应的最大元素。让我们举一个例子:

    假设在堆栈中,您正在推送元素3。然后在您的优先队列pQ中,您将提供3。此时,3将是堆栈中与3对应的最大元素

    让我们在堆栈S中插入5个。提供5个pQ。由于5>3,pQ中的元素顺序为5 3。 让我们推出4英寸S。同时提供4英寸pQ。pQ现在将包含元素:5 4 3。如果执行getMax(),则会得到pQ的头部,这需要O(1)个时间,因为最大元素始终位于pQ的顶部

    对于S.pop(),如果以LinkedList的形式存储pQ,则可以在O(1)时间内从pQ中删除相应的弹出元素。因此,所有这些操作实际上需要O(1)个时间

    按照同样的逻辑,您也可以在O(1)时间内完成popMax()。只需返回pQ的头部,并从堆栈中删除相应的节点,这同样可以在O(1)的时间内完成

    以下是两者的结构:

    public class Node{
        int data;
        Node next;
        Node(int data){
            this.data = data;
            next = null;
        }
    }
    
    PriorityQueue<Node> pQ = new PriorityQueue<Node>();
    Stack<Node> S = new Stack<Node>();
    
    公共类节点{
    int数据;
    节点下一步;
    节点(int数据){
    这个数据=数据;
    next=null;
    }
    }
    PriorityQueue pQ=新的PriorityQueue();
    堆栈S=新堆栈();
    
    除非堆栈以某种方式排序,否则没有比O(n)更快的方法了!?您可以使用多线程解决方案,但可能就是这样。除非堆栈以某种方式排序,否则没有比O(n)更快的方法了!?您可以使用多线程解决方案,但可能就是这样。如果您这样做,为什么不在堆栈类中保存一个
    int max
    ,而不是另一个堆栈@Xander如果您只保存一个
    max
    ,那么当您弹出一个元素时,您必须遍历整个堆栈才能再次找到
    max
    。@Xander然后我们必须遍历整个堆栈才能再次找到max。@Xander当从堆栈中删除一个元素时,如果它是max,您需要遍历整个堆栈以找到前一个第二大值,即新的最大值。您没有正确重写push方法这一事实使得此操作非常容易出错。如果您这样做,为什么不在stack类中保存一个
    int max
    ,而不是另一个堆栈@Xander如果您只保存一个
    max
    ,那么当您弹出一个元素时,您必须遍历整个堆栈才能再次找到
    max
    。@Xander然后我们必须遍历整个堆栈才能再次找到max。@Xander