forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1851-Minimum-Interval-to-Include-Each-Query.cs
44 lines (37 loc) · 1.2 KB
/
1851-Minimum-Interval-to-Include-Each-Query.cs
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
36
37
38
39
40
41
42
43
44
public class Solution
{
public int[] MinInterval(int[][] intervals, int[] queries)
{
var q = queries.Length;
var indexDict = new int[q][];
var index = 0;
foreach (var query in queries)
{
indexDict[index] = new int[2] { query, index };
index++;
}
Array.Sort(indexDict, (a, b) => a[0] - b[0]);
Array.Sort(intervals, (a, b) => a[0] - b[0]);
var pq = new PriorityQueue<int[], int>();
var result = new int[queries.Length];
index = 0;
foreach (var query in indexDict)
{
var resultIndex = query;
var calResult = -1;
while (index < intervals.Length && intervals[index][0] <= resultIndex[0])
{
var curr = intervals[index];
pq.Enqueue(new int[2] { curr[1] - curr[0] + 1, curr[1] }, curr[1] - curr[0] + 1);
index++;
}
while (pq.Count > 0 && pq.Peek()[1] < resultIndex[0])
{
pq.Dequeue();
}
calResult = pq.Count > 0 ? pq.Peek()[0] : -1;
result[resultIndex[1]] = calResult;
}
return result;
}
}