Skip to content

Commit 86a0d60

Browse files
committed
leetcode.com 1851. Minimum Interval to Include Each Query
문제 링크: https://leetcode.com/problems/minimum-interval-to-include-each-query/description/
1 parent 5bb1e73 commit 86a0d60

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from typing import List
2+
import heapq
3+
4+
class Solution:
5+
def minInterval(self, intervals: List[List[int]], queries: List[int]) -> List[int]:
6+
length = len(intervals)
7+
intervals.sort()
8+
heap = []
9+
dict = {}
10+
idx = 0
11+
12+
for q in sorted(queries):
13+
while idx < length and intervals[idx][0] <= q:
14+
left, right = intervals[idx]
15+
l = right - left + 1
16+
heapq.heappush(heap, [l, right])
17+
idx += 1
18+
19+
while heap and heap[0][1] < q:
20+
heapq.heappop(heap)
21+
22+
dict[q] = heap[0][0] if heap else -1
23+
24+
return [dict[q] for q in queries]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from unittest import TestCase
2+
from main import Solution
3+
4+
class TestSolution(TestCase):
5+
def test1_min_interval(self):
6+
sol = Solution()
7+
self.assertEqual([3,3,1,4], \
8+
sol.minInterval([[1,4],[2,4],[3,6],[4,4]], [2,3,4,5]))
9+
10+
def test2_min_interval(self):
11+
sol = Solution()
12+
self.assertEqual([2,-1,4,6], \
13+
sol.minInterval([[2,3],[2,5],[1,8],[20,25]], [2,19,5,22]))

0 commit comments

Comments
 (0)