微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

意外行为 java 优先级队列对象添加一次但轮询两次怎么可能?

如何解决意外行为 java 优先级队列对象添加一次但轮询两次怎么可能?

import java.util.*;

public class Main {
    public static void main (String[] args) {
        Solution solution = new Solution();
        int[] res = solution.assignTasks(new int[]{3,3,2},new int[]{1,2,1,2});
    }
}
class Solution {
    public int[] assignTasks(int[] servers,int[] tasks) {
        PriorityQueue<Pair> free = new PriorityQueue(); // (wt,id,time)
        PriorityQueue<Pair> busy = new PriorityQueue(); // (time,wt,id)
        
        for (int i = 0; i < servers.length; i++) {
            free.add(new Pair(servers[i],i,0));
            System.out.println("Free Added " + i + " " + servers[i] + " " + i + " " + 0 + " " + free.size());
        }
        
        int[] ans = new int[tasks.length];
        for (int i = 0; i < tasks.length; i++) {
            Pair b = busy.peek();
            while (b != null && b.a <= i) {
                busy.poll();
                System.out.println("Busy Polled " + i + " " + b.a + " " + b.b + " " + b.c + " " + busy.size());
                free.add(new Pair(b.b,b.c,b.a));
                System.out.println("Free Added " + i + " " + b.b + " " + b.c + " " + b.a + " " + free.size());
                b = busy.peek();
            }
            Pair p = free.poll();
            
            int wt = p.a;
            int id = p.b;
            
            // int time = p.c;
            System.out.println("Free Polled " + i + " " + p.a + " " + p.b + " " + p.c + " " + free.size());
            
            ans[i] = id;
            busy.add(new Pair(i + tasks[i],id));
            System.out.println("Added to Busy " + i + " " + (i + tasks[i]) + " " + p.a + " " + p.b + " " + busy.size());
        }
        
        return ans;
    }
}
class Pair implements Comparable<Pair> {
    int a,b,c;
    Pair(int x,int y,int z) {
        a = x;
        b = y;
        c = z;
    }
    public int compareto(Pair p) {
         if (this.a == p.a) {
             if (this.b == p.b) return this.c - p.c;
             return this.b = p.b;
         }
         return this.a - p.a;
     }
    
}

我在优先级队列中使用 Pair 类插入了三元组一对从队列中轮询了两次并且只插入了一次。这怎么可能? 我还附加了我添加到调试中的打印语句的控制台输出。突出显示异常行为。

代码输出:(注意输出中的行为。突出显示它(3,0)轮询两次?)

Free Added 0 3 0 0 1       (Added to the queue)
Free Added 1 3 1 0 2
Free Added 2 2 2 0 3
Free Polled 0 2 2 0 2
Added to Busy 0 1 2 2 1
Busy Polled 1 1 2 2 0
Free Added 1 2 2 1 3
Free Polled 1 2 2 1 2
Added to Busy 1 3 2 2 1
Free Polled 2 3 0 0 1     (Polled 1st time)
Added to Busy 2 5 3 0 2
Busy Polled 3 3 2 2 1
Free Added 3 2 2 3 2
Free Polled 3 2 2 3 1
Added to Busy 3 5 2 2 2
Free Polled 4 3 0 0 0     (Polled 2nd time)
Added to Busy 4 5 3 0 3
Busy Polled 5 5 3 0 2
Free Added 5 3 0 5 1
Busy Polled 5 5 3 0 1
Free Added 5 3 0 5 2
Busy Polled 5 5 3 2 0
Free Added 5 3 2 5 3
Free Polled 5 3 0 5 2
Added to Busy 5 7 3 0 1

问题来自最近的 Leetcode 竞赛,现在已经结束。链接到问题的 discussion 部分。链接question IDE 链接 run

解决方法

尚不完全清楚,但我认为您的问题是由 compareTo 的错误实现引起的。

public int compareTo(Pair p) {
     if (this.a == p.a) {
         if (this.b == p.b) return this.c - p.c;
         return this.b = p.b;
     }
     return this.a - p.a;
 }

问题 #1

return this.b = p.b;

这会返回 p.b 并作为副作用将 p.b 分配给 this.b。这是完全错误的。

您的意思可能是return this.b - p.b;

请注意,由于您的 compareTo 方法正在更改 Pair 对象之一,因此结果将会混乱。这可能就是为什么某些 Pair 对象出现会被多次返回的原因。 (实际上,它们是不同的 Pair 对象……但是您已经对它们进行了变异,以便它们在 c 字段中具有相同的值。)

问题 #2

表达式 this.c - p.c 在边缘情况下给出了错误的答案。例如,this.c 非常大而 p.c 为负,那么减法可能会溢出到一个负数,表示 this.c “小于”p.c ...它不是。

有关这方面的更多信息,请参阅 Java Integer compareTo() - why use comparison vs. subtraction?

以下是比较两个 int 值以给出与 compareTo 合同兼容的结果的几种正确方法:

  1. 普通话:

    if (this.c == p.c) {
        return 0;
    } else if (this.c < p.c) {
        return -1;
    } else {
        return +1;
    }
    
  2. 使用条件表达式:

    return (this.c == p.c) ? 0 : ((this.c < p.c) ? -1 : +1);
    
  3. 使用 Integer.compare

    return Integer.compare(this.c,p.c);
    

正确的实现

如果我们假设 Pair 对象应该是不可变的,那么这是一个正确的1 实现:

class Pair implements Comparable<Pair> {
    final int a,b,c;

    Pair(int x,int y,int z) {
        a = x;
        b = y;
        c = z;
    }

    public int compareTo(Pair p) {
        if (this.a == p.a) {
            if (this.b == p.b) {
                return Integer.compare(this.c,p.c);
            } else {
                return Integer.compare(this.b,p.b);
            }
        } else {
            return Integer.compare(this.a,p.a);
        }
    }
}

问:为什么将字段声明为 final
答:为了防止在 Pair 类本身以及该类及其字段可访问的其他类中对字段进行意外更改。

问:为什么要使用 if ... else
答:因为比较清楚。并且因为 JIT 编译器在任何一种情况下都应该生成等效(最佳)的代码。

问:compare 怎么样?方法调用会不会效率低下?
答:应该不会。方法调用可能会被 JIT 编译器内联。

问:效率重要吗?
答:在您的示例中,这无关紧要。在一般情况下,它可能无关2


1 - 类名 Pair 也不正确。它应该是 Triple
2 - 只有在 1) 对代码进行基准测试,2) 对其进行分析,以及 3) 确定 compareTo 方法确实是一个性能热点之后,您才应该尝试手动优化它。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。