1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| class Solution { public int[] minInterval(int[][] intervals, int[] queries) { int n = intervals.length, m = queries.length; Arrays.sort(intervals, (a, b) -> a[0] - b[0]); int[][] qs = new int[m][2]; for (int i = 0; i < m; ++i) { qs[i] = new int[]{queries[i], i}; } Arrays.sort(qs, (a, b) -> a[0] - b[0]); int[] ans = new int[m]; Arrays.fill(ans, -1); PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[0] - b[0]); int i = 0; for (int[] q : qs) { while (i < n && intervals[i][0] <= q[0]) { int a = intervals[i][0], b = intervals[i][1]; pq.offer(new int[]{b - a + 1, b}); ++i; } while (!pq.isEmpty() && pq.peek()[1] < q[0]) { pq.poll(); } if (!pq.isEmpty()) { ans[q[1]] = pq.peek()[0]; } } return ans; } }
|