Skip to content

Commit 7c6cbb6

Browse files
authored
Create 1851-minimum-interval-to-include-each-query.kt
1 parent db117dd commit 7c6cbb6

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
fun minInterval(intervals: Array<IntArray>, _queries: IntArray): IntArray {
3+
intervals.sortWith(compareBy( {it[0]}, { it[1]}))
4+
val queries = _queries.withIndex().sortedBy { it.value }
5+
val minHeap = PriorityQueue<IntArray> (compareBy( { it[0] }, { it[1] }))
6+
val res = IntArray (queries.size)
7+
8+
var i = 0
9+
queries.forEach { (idx, q) ->
10+
while (i < intervals.size && intervals[i][0] <= q) {
11+
val (l, r) = intervals[i]
12+
minHeap.add(intArrayOf(r - l + 1, r))
13+
i++
14+
}
15+
16+
while (minHeap.isNotEmpty() && minHeap.peek()[1] < q)
17+
minHeap.poll()
18+
19+
res[idx] = if (minHeap.isEmpty()) -1 else minHeap.peek()[0]
20+
}
21+
22+
return res
23+
}
24+
}

0 commit comments

Comments
 (0)