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,3,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], wt, 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;
}
}
I have inserted triplets using Pair class in the priority queue and one pair is polled from the queue two times and was inserted only once. How it could be possible? I am also attaching the console output for print statements I added to debug. Highlighted the unusual behavior.
The output of Code: (Note the behavior in output. Highlighting it (3,0,0) polled twice?)
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
The question was from a recent Leetcode Contest that was over now. Link to the discussion section of the question. Link to question Link to IDE run