2
public int[] minInterval(int[][] intervals, int[] queries) {4
Arrays.sort(intervals, (a, b) -> a[0] - b[0]);6
int[][] q = new int[queries.length][2];7
for (int i = 0; i < queries.length; i++) {11
// sort the queries by query val12
Arrays.sort(q, (a, b) -> a[1] - b[1]);13
// store the minimum intervals in the priority queue, min heap14
Queue<int[]> pq = new PriorityQueue<>((a, b) -> (a[1] - a[0]) - (b[1] - b[0]));15
int[] result = new int[queries.length];17
for (int i = 0; i < q.length; i++) {20
// if start is less than query val, then add to pq21
while (j < intervals.length && intervals[j][0] <= val) pq.offer(intervals[j++]);22
// anything which has the end lesser than the val then remove it23
while (!pq.isEmpty() && pq.peek()[1] < val) pq.poll();24
// add difference to the result25
result[index] = pq.isEmpty() ? -1 : pq.peek()[1] - pq.peek()[0] + 1;