Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1193 from ShrujanKotturi/skotturi/1851…
Browse files Browse the repository at this point in the history
…-Minimum-Interval-to-Include-Each-Query

Create 1851-Minimum-Interval-to-Include-Each-Query.cs
  • Loading branch information
Ahmad-A0 authored Sep 30, 2022
2 parents ed962bb + 6720e2b commit 77f932e
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions csharp/1851-Minimum-Interval-to-Include-Each-Query.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;

}
}

0 comments on commit 77f932e

Please sign in to comment.